You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetSuite采购订单自动匹配Item Vendor Setting字段、MOQ校验需求及客户端脚本故障排查求助

Solution for Purchase Order MOQ Validation & Auto-Population

1. Auto-Populating "Item Vendor id" Field

Yep, you’ll need a client script to automatically select the matching "Item Vendor Setting" record once the Vendor and Item are chosen on the PO line. Filtered list fields don’t auto-select matches by default—this requires custom logic to find the right record and set it in the field.

Sample Script for Auto-Population & MOQ Validation

Here’s a revised client script that handles both auto-population and your MOQ validation requirement, plus fixes the issues in your original code:

/**
*@NApiVersion 2.1
*@NScriptType ClientScript
*@NModuleScope Public
*/
define(['N/currentRecord', 'N/search'], function (currentRecord, search) {

    // Helper to get the PO's main vendor
    function getPOVendor(currentRec) {
        return currentRec.getValue({ fieldId: 'entity' });
    }

    function validateLine(context) {
        var currentRec = context.currentRecord;
        var sublistName = context.sublistId;

        if (sublistName === 'item') {
            // Grab line item details
            var lineItem = currentRec.getCurrentSublistValue({
                sublistId: sublistName,
                fieldId: 'item'
            });
            var poVendor = getPOVendor(currentRec);
            var lineQty = currentRec.getCurrentSublistValue({
                sublistId: sublistName,
                fieldId: 'quantity'
            });

            // Exit if required fields are missing
            if (!lineItem || !poVendor) {
                return true; // Let NetSuite handle required field checks
            }

            // Search for matching Item Vendor Setting record
            var itemVendorSearch = search.create({
                type: 'customrecord_item_vendor_setting', // Replace with your actual custom record ID
                filters: [
                    ['custrecord_ivs_vendor', 'is', poVendor],
                    'and',
                    ['custrecord_ivs_item', 'is', lineItem]
                ],
                columns: ['custrecord_ivs_min_order_qty']
            });

            var matchingRecordId = null;
            var moq = 0;

            itemVendorSearch.run().each(function(result) {
                matchingRecordId = result.id;
                moq = parseFloat(result.getValue({ name: 'custrecord_ivs_min_order_qty' })) || 0;
                return false; // Stop after first match (assumes one per vendor-item pair)
            });

            // Auto-populate the Item Vendor id field if a match exists
            if (matchingRecordId) {
                currentRec.setCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: 'custcol_item_vendor_id', // Replace with your actual field ID
                    value: matchingRecordId
                });

                // Validate quantity against MOQ
                if (moq > 0 && parseFloat(lineQty) < moq) {
                    alert(`Error: Minimum order quantity for this item from vendor ${poVendor} is ${moq}. Please adjust your order quantity.`);
                    return false; // Block invalid line from being added
                }
            } else {
                // Clear the field if no match is found
                currentRec.setCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: 'custcol_item_vendor_id',
                    value: ''
                });
            }
        }

        return true; // Allow line to be saved if no errors
    }

    function fieldChanged(context) {
        var currentRec = context.currentRecord;
        var sublistName = context.sublistId;
        var fieldId = context.fieldId;

        // Auto-populate immediately when Item or Vendor changes (better UX)
        if (sublistName === 'item' && (fieldId === 'item' || fieldId === 'entity')) {
            var lineItem = currentRec.getCurrentSublistValue({ sublistId: sublistName, fieldId: 'item' });
            var poVendor = getPOVendor(currentRec);

            if (lineItem && poVendor) {
                var itemVendorSearch = search.create({
                    type: 'customrecord_item_vendor_setting',
                    filters: [
                        ['custrecord_ivs_vendor', 'is', poVendor],
                        'and',
                        ['custrecord_ivs_item', 'is', lineItem]
                    ]
                });

                var matchingRecordId = null;
                itemVendorSearch.run().each(function(result) {
                    matchingRecordId = result.id;
                    return false;
                });

                currentRec.setCurrentSublistValue({
                    sublistId: sublistName,
                    fieldId: 'custcol_item_vendor_id',
                    value: matchingRecordId || ''
                });
            }
        }
    }

    return {
        validateLine: validateLine,
        fieldChanged: fieldChanged // Optional, for real-time auto-population
    };
});

2. Key Fixes to Your Original Script

Looking at your code, these were the main issues causing problems:

  • Backwards Condition: Your if (!recsub && !reclineitem) check meant the code only ran when both subsidiary and item were empty—this should be if (recsub && reclineitem) to trigger when both fields have values.
  • Wrong Custom Record: You were searching for customrecord_fc_item_subsi_fields instead of your "Item Vendor Setting" record.
  • Missing MOQ Logic: The script didn’t include checks to validate the order quantity against the stored MOQ.
  • No Error Blocking: The validateLine function needs to return false to prevent invalid lines from being saved, which your code didn’t handle.

3. Important Notes

  • Replace placeholders like customrecord_item_vendor_setting, custrecord_ivs_vendor, and custcol_item_vendor_id with your actual custom record/field IDs from NetSuite.
  • The fieldChanged function is optional but gives users immediate feedback by populating the "Item Vendor id" field right when they select an Item/Vendor, instead of waiting for line save.
  • Ensure your "Item Vendor Setting" records have unique Vendor-Item pairs to avoid multiple matches in the search.

内容的提问来源于stack exchange,提问作者Mingo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 13:02:37