Setting Pop-up message Using Client Script

Problem: How to set pop-up message using client script when the PO number is already used in a Sales order

Solution

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*******************************************************************
 *  Client script for setting the Netsuite warning dialogue box
 ****************************************************************
 *
 * Date: 03/03/2022
 *
 * Author:  Jobin and Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 * Revision
 *
 * Description: Setting an alert box if PO number already exists.
 *
 ***************************************************************/
define(['N/currentRecord', 'N/record', 'N/ui/dialog', 'N/search', 'N/runtime'],
    function (currentRecord, record, dialog, search, runtime) {

        var sc;

        /**
         *  @param function to Create search to check PO number already exist or not
         *  @param returns search result
         */
        function salesOrderSearch(newPORecID) {
            try {
                var salesorderSearchObj = search.create({
                    type: "salesorder",
                    filters:
                    //5CDA8YCJ
                        [
                           
                            ["type","anyof","SalesOrd"],
                            "AND",
                            ["custbody_cust_po_number","is",newPORecID],
                            "AND",
                            ["mainline","is","T"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "custbody_cust_po_number", label: "PO Number NEW"})
                        ]

                });
                var searchResultCount = salesorderSearchObj.runPaged().count;
                //console.log("salesorderSearchObj result count", searchResultCount);
                var resultSet = salesorderSearchObj.run();
                return searchResultCount;
            } catch (e) {
                console.log("error @ function search")

            }
        }

        /**
         * Function to be executed at pageInit.
         * @description the function pop ups a dialog box when PO exists in the copy context
         *
         */

        function pageInit(scriptContext) {
            sc = scriptContext.mode;
            //log.debug("enteered")
           // console.log("context values", sc);
            try {
                if (sc === 'copy') {
                    var newPORecID = scriptContext.currentRecord.getValue({
                        fieldId: 'custbody_cust_po_number'
                    });
                    //console.log("newPORecID", newPORecID);
                    if (newPORecID!=='')
                    var searchResult1 = salesOrderSearch(newPORecID)
                    //console.log('Searchres', searchResult1)
                    if (searchResult1 > 0) {
                        var options = {
                            title: "PO number Popup Message",
                            message: "This PO Number has already been used"
                        };
                        dialog.alert(options);
                    }

                }
            } catch (e) {

                log.debug("error @ pageInit")
            }

        }

        /**
         * Function to be executed when field is changed.
         * @description the function pop ups a dialogue box if PO number already exists i
         *
         */

        function fieldChanged(scriptContext) {
            try {
                var currentRec = scriptContext.currentRecord;
                if (runtime.executionContext === "USERINTERFACE") {
                    if (sc === 'create' || sc === 'copy') {
                        var newRecord = scriptContext.currentRecord;
                        if (scriptContext.fieldId === 'custbody_cust_po_number') {
                            var newPORecID = scriptContext.currentRecord.getValue({
                                fieldId: 'custbody_cust_po_number'
                            });
                            console.log("newPORecID", newPORecID);
                            if (newPORecID!=='') {
                                var searchResult1 = salesOrderSearch(newPORecID)
                                console.log('Searchres', searchResult1)

                                if (searchResult1 > 0) {

                                    var options = {
                                        title: "PO number Popup Message",
                                        message: "This PO Number has already been used"
                                    };
                                    dialog.alert(options);
                                }
                            }
                        }
                    }
                }

            } catch (e) {
                console.log("error @ field change")
            }
        }

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

    });

Leave a comment

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