Validate item line and restrict if the shipping method is items restricted shipping method.

Consider that there is a custom body field in the item record named Restricted Shipping Method (custitem_jj_restrict_shipmeth). To validate and restrict if the transaction’s shipping method is the item’s restricted shipping method, use the client script given below.

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/search'],
    /**
     * @param search 
     * @returns 
     */
    function (search) {
        "use strict"
        /**
         * variable to store restricted item details
         */
        let RESTRICTED_ITEMS = []
        let CONTEXT_MODE;
        const getItemLists = (value) => {
            let itemSearchObj = search.create({
                type: search.Type.ITEM,
                filters:
                    [
                        [`custitem_jj_restrict_shipmeth`, "contains", value],
                        "AND",
                        ["isinactive", "is", "F"]
                    ],
                columns:
                    [
                        search.createColumn({ name: "itemid", label: "Name" }),//0
                        search.createColumn({ name: "internalid", label: "Internal ID" }),//1
                    ]
            });
            let searchResult = []
            itemSearchObj.run().each(function (result) {
                let objResult = {};
                objResult["Name"] = {
                    value: result.getValue(itemSearchObj.columns[0]),
                    text: result.getText(itemSearchObj.columns[0])
                };
                objResult["Internal ID"] = {
                    value: result.getValue(itemSearchObj.columns[1]),
                    text: result.getText(itemSearchObj.columns[1])
                };
                searchResult.push(objResult)
                return true;
            });
            return searchResult
        }
        /**
         * Function to be executed after page is initialized.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
         *
         * @since 2015.2
         */
        function pageInit(scriptContext) {
            try {
                CONTEXT_MODE = scriptContext.mode;
                let currentRec = scriptContext.currentRecord;
                if (CONTEXT_MODE === "copy" && currentRec.type === "salesorder") {
                    let shippingMethod = currentRec.getValue({
                        fieldId: 'shipmethod'
                    });
                    if (shippingMethod) {
                        RESTRICTED_ITEMS = getItemLists(shippingMethod)
                    } else {
                        RESTRICTED_ITEMS = [];
                    }
                }
            } catch (err) {
                console.error("error@pageInit", err)
            }


        }


        /**
         * Function to be executed when field is changed.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.sublistId - Sublist name
         * @param {string} scriptContext.fieldId - Field name
         * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
         * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
         *
         * @since 2015.2
         */
        function fieldChanged(scriptContext) {
            let currentRec = scriptContext.currentRecord;
            if (CONTEXT_MODE && (CONTEXT_MODE === "create" || CONTEXT_MODE === "copy") && currentRec.type === "salesorder") {
                if (scriptContext.fieldId === "shipmethod") {
                    let shippingMethod = currentRec.getValue({
                        fieldId: 'shipmethod'
                    });
                    if (shippingMethod) {
                        RESTRICTED_ITEMS = getItemLists(shippingMethod)
                    } else {
                        RESTRICTED_ITEMS = []
                    }
                }
            }
        }


        /**
         * Validation function to be executed when sublist line is committed.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.sublistId - Sublist name
         *
         * @returns {boolean} Return true if sublist line is valid
         *
         * @since 2015.2
         */
        function validateLine(scriptContext) {
            try {
                let returnVal = true;
                if (CONTEXT_MODE && (CONTEXT_MODE === "create" || CONTEXT_MODE === "copy")) {
                    let isShipMethRestrict = false, alertMessage = "";
                    let currentRec = scriptContext.currentRecord;
                    if (currentRec.type === "salesorder" && scriptContext.sublistId === 'item') {
                        let itemId = currentRec.getCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'item'
                        });
                        if (RESTRICTED_ITEMS.length) {
                            isShipMethRestrict = RESTRICTED_ITEMS.some(obj => obj["Internal ID"].value === itemId);
                        }
                        if (isShipMethRestrict) {
                            returnVal = false;
                            alertMessage = "The item cannot be selected due to restrictions on the shipping method.";
                        }
                    }
                    if (!returnVal) {
                        alert(alertMessage)
                    }
                }
                return returnVal
            } catch (err) {
                console.error("error@validateLine", err);
                return true;
            }
        }


        return {
            pageInit: pageInit,
            fieldChanged: fieldChanged,
            validateLine: validateLine
        };


    });

Leave a comment

Your email address will not be published. Required fields are marked *