Client Script to set the Item line fields based on Conditions in Purchase Order

/**

 * @NApiVersion 2.1

 * @NScriptType ClientScript

 * @NModuleScope SameAccount

 */

/********************************************************************************************************************************************************************

 * IBS Electronics Group-USA-NS

 *

 * ${IEGUN-510} : ${Set Requsted Order by Line Key on Purchase Order}

 *

 *********************************************************************************************************************************************************************

 *

 * Author: Jobin & Jismi

 *

 * Date Created : 16-January-2024

 *

 * Description :This Client script is used to set the request order based on the request key for each item line in Purchase order.

 *

 * REVISION HISTORY

 *

 * @version 1.0 IEGUN-510 : 16-January-2024 : Created the initial build by JJ0170

 *

 ***********************************************************************************************************************************************************************/

define([‘N/currentRecord’, ‘N/search’, ‘N/runtime’],

    /**

     * @param{currentRecord} currentRecord

     * @param{search} search

     * @param{runtime} runtime

     */

    function (currentRecord, search, runtime) {

        /**

         * Function to check whether the given parameter argument has value on it or is it empty.

         * @param {*} parameter – parameter which contains/references some values

         * @returns {boolean} – true if there exist a value else false

         */

        function checkForParameter(parameter) {

            try {

                if (parameter !== “” && parameter !== null && parameter !== undefined && parameter !== false && parameter !== “null” && parameter !== “undefined” && parameter !== ” “ && parameter !== ‘false’) {

                    return true;

                }

                else {

                    return false;

                }

            }

            catch (e) {

                console.error(“Error @ checkForParameter”, e);

                return false;

            }

        }

        /**

         * Function to fetch the sales order id based on the item line unique key

         * @param {number} requestKey – Line unique key

         * @returns {number} requestOrder – Internal id of the sales order

         */

        function fetchRequestOrderFromSalesOrder(requestKey) {

            try {

                let salesorderSearchObj = search.create({

                    type: “salesorder”,

                    filters:

                        [

                            [“type”, “anyof”, “SalesOrd”],

                            “AND”,

                            [“mainline”, “is”, “F”],

                            “AND”,

                            [“lineuniquekey”, “equalto”, requestKey]

                        ],

                    columns:

                        [

                            search.createColumn({ name: “internalid”, summary: “GROUP”, label: “Internal ID” })

                        ]

                });

                let searchResultCount = salesorderSearchObj.runPaged().count;

                let requestOrder = ;

                if (searchResultCount > 0) {

                    salesorderSearchObj.run().each(function (result) {

                        requestOrder = result.getValue({ name: “internalid”, summary: “GROUP”, label: “Internal ID” });

                    });

                }

                return requestOrder;

            }

            catch (e) {

                console.error(“Error @ fetchRequestOrderFromSalesOrder”, e);

                return null;

            }

        }

        /**

         * Function to set the request order in the purchase order item line

         * @param {object} poRec – object of the transaction

         * @param {number} requestKey – line unique key

         */

        function setRequestOrderOnItemLine(poRec, requestKey) {

            try {

                let requestOrder = ;

                if (checkForParameter(requestKey)) {

                    requestOrder = fetchRequestOrderFromSalesOrder(requestKey);

                }

                //set the request order

                poRec.setCurrentSublistValue({

                    sublistId: “item”,

                    fieldId: “custcol_mhi_ibs_request_order”,

                    value: requestOrder

                });

            } catch (e) {

                console.error(“Error @ setRequestOrderOnItemLine”, e);

            }

        }

        /**

         * 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) {

            try {

                if (scriptContext.sublistId === “item” && scriptContext.fieldId === “custcol_mhi_ibs_request_key” && runtime.executionContext == “USERINTERFACE”) {

                    let poRec = scriptContext.currentRecord;

                    let requestKey = poRec.getCurrentSublistValue({

                        sublistId: “item”,

                        fieldId: “custcol_mhi_ibs_request_key”

                    });

                    setRequestOrderOnItemLine(poRec, requestKey);

                }

            }

            catch (e) {

                console.error(“Error @ fieldChanged”, e);

            }

        }

        return {

            fieldChanged: fieldChanged,

        };

    });

Leave a comment

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