Restrict save by throwing a message if any of the items in the sales order is restricted to the transactions shipping method.

Consider that there is a custom body field in the item record named Restricted Shipping Method (custitem_jj_restrict_shipmeth). To check if there are any items that are restricted to this shipping method, and restrict the save, use the user event below.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/search'],
    /**
     * @param search 
     * @returns 
     */
    (search) => {
        /**
         * saved search to fetch item list 
         * @param value 
         * @returns 
         */
        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
        }
        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {
            let isShipMethRestrict = false;
            try {
                let currentRec = scriptContext.newRecord;
                if (currentRec.type === "salesorder" && (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.COPY)) {
                    let lineCount = currentRec.getLineCount({
                        sublistId: 'item'
                    });
                    let shippingMethod = currentRec.getValue({
                        fieldId: 'shipmethod'
                    });
                    let shipMethRestrictItem = getItemLists(shippingMethod);
                    let itemArray = [];
                    for (let i = 0; i < lineCount; i++) {
                        let itemId = currentRec.getSublistValue({
                            fieldId: 'item',
                            sublistId: 'item',
                            line: i
                        });
                        itemArray.push(itemId);
                    }
                    isShipMethRestrict = shipMethRestrictItem.length ? shipMethRestrictItem.some(obj => [...new Set(itemArray)].some(el => el === obj["Internal ID"].value)) : false
                }
            } catch (err) {
                log.error("error@beforeSubmit", err)
            }
            if (isShipMethRestrict) {
                throw ({ message: 'Some of the items are restricted for this shipping method.' });
            }
        }
        return { beforeSubmit }
    });

Leave a comment

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