Double Entry of the Same Customer PO

Requirement

Need to prevent the double-entry of the same Customer PO. We can prevent this by using a client script. We can show an alert message while trying to save the record This alert also prevents the user from saving the sales order record.To check the double entry, we will check if there are any already existing sales order that has the same PO# number. If so, then we will restrict the SO creation.

Solution

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */


define(['N/currentRecord', 'N/record', 'N/search'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{search} search
     */
    function (currentRecord, record, search) {

        /**
         * 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
         */


        //Search to check if duplicate PO and Customer combination exists.
        // It returns the count, if count >0, duplicate PO nad customer combination exists. If count =0, combination does not exists

        function checkDuplicatePO(poNum, customer) {
            var salesorderSearchObj = search.create({
                type: "salesorder",
                filters:
                    [
                        ["type", "anyof", "SalesOrd"],
                        "AND",
                        ["otherrefnum", "equalto", poNum],
                        "AND",
                        ["customer.internalid", "anyof", customer],
                        "AND",
                        ["mainline", "is", "T"]
                    ],
                columns:
                    [
                        search.createColumn({name: "internalid", label: "Internal ID"}),
                        search.createColumn({name: "otherrefnum", label: "PO/Check Number"})
                    ]
            });
            var searchRes = salesorderSearchObj.runPaged().count;
            console.log('Counnt in search', searchRes)
            return searchRes;
        }


        /**
         * Validation function to be executed when record is saved.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @returns {boolean} Return true if record is valid
         *
         * @since 2015.2
         */
        function saveRecord(scriptContext) {

            try {

                //Get Current Record
                var currentRec = scriptContext.currentRecord;


                //Get po#
                var poNum = currentRec.getValue({
                    fieldId: 'otherrefnum'
                });
                //Get Customer
                var customer = currentRec.getValue({
                    fieldId: 'entity'
                });
                //Get subsidiary
                var sub = currentRec.getValue({
                    fieldId: 'subsidiary'
                });


                //Following code executes only if Po# and customer value is present
                if ((poNum != '') && (customer != '')) {

                    //Check the subsidiary for Australia , US and UK
                    if (sub == 2 || sub == 7 || sub == 9) {
                        

                        if (!(scriptContext.currentRecord.id)) {

                            //Calling the function to check if duplicate PO exists
                            var searchResultCount = checkDuplicatePO(poNum, customer);

                            //If duplicate PO exists searchResultCount will be greater than 0, and will displays an alert
                            if (searchResultCount > 0) {
                                alert('You have entered a duplicate PO# for this customer')
                                return false
                            }
                            return true;
                        }
                        return true;
                    }
                    return true;
                }
                 return true;

            } catch (e) {
                console.log('Error@SveRecord', e)
            }

        }

        return {
            saveRecord: saveRecord
        };

    });

Leave a comment

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