Merge duplicate items in sales orders during CSV import

Jira code : BTN-604

During CSV import for sales order, when there is item duplication in single SO, merge it.

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

/*******************************************************************************
 * BALAJI WIRELESS
 * BTN-604 If duplicate SO, merge it
 *
 * **************************************************************************
 * 
 * Date: 05/02/2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : During CSV import for sales order, when there is item duplication in single SO, 
 *       merge it.
 *Date created : 05 Feb 2019
 * 
 * REVISION HISTORY
 * 
 * Revision 1.0 05/02/2019 Maria : Created
 * 
 ******************************************************************************/
define(['N/runtime','N/record'],

function(runtime,record) {
   
    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {string} scriptContext.type - Trigger type
     * @param {Form} scriptContext.form - Current form
     * @Since 2015.2
     */
    
    function beforeSubmit(scriptContext) {

        try{
            // To get the Context CSV import
            var currentContext = runtime.executionContext;   

                if(currentContext == 'CSVIMPORT'){
                
                    // To get SO line item count
                    var rec = scriptContext.newRecord;
                    var linecount = rec.getLineCount({
                        sublistId: 'item'
                    });

                    // Take the first line item and compare with other items
                    for (var i = 0; i<linecount; i++){
                        var cur = rec.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            line: i
                        });

                        // Take the second line item 
                        for(var j = i+1; j<linecount; j++){
                            var ref = rec.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'item',
                                line: j
                            });

                            // line item matches duplicates found
                            if(cur == ref && cur != null && ref != null){
                                logme("Duplicate found",cur);

                                // Takes the quantity of first line item
                                var quantity1 = rec.getSublistValue({
                                    fieldId: "quantity",
                                    sublistId: 'item',
                                    line: i
                                });

                                //Takes quantity of second line item
                                var quantity2 = rec.getSublistValue({
                                    fieldId: "quantity",
                                    sublistId: 'item',
                                    line: j
                                });

                                // Calculates total quantity
                                var totalquantity = quantity1 + quantity2;

                                // Set total quantity to first item
                                rec.setSublistValue({
                                    sublistId: 'item',
                                    fieldId: 'quantity',
                                    line: i,
                                    value: totalquantity
                                });

                                // removes the second(duplicate)
                                var removedline = rec.removeLine({
                                    sublistId: 'item',
                                    line: j,
                                    ignoreRecalc: true
                                });
                            }
                        }
                    }
                }

            } catch (e) {
                    logme('TRY@updateSO', getError(e));
                }

    }


    return {
        //beforeLoad: beforeLoad,
        beforeSubmit: beforeSubmit
        //afterSubmit: afterSubmit
    };


        /*******************************************************************************
         * Log these data
         * 
         * @param title
         * @param details
         * @returns
         * 
         * Created on 31-Jan-2019 by maria
         */
        function logme(title, details) {
            log.debug({
                title: title,
                details: details
            });
        }


        /*******************************************************************************
         * return error
         * 
         * @param e
         * @returns
         * 
         * Created on 31-Jan-2019 by maria
         */
        function getError(e) {
            var stErrMsg = '';
            if (e.getDetails != undefined) {
                stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>' +
                    e.getStackTrace();
            } else {
                stErrMsg = '_' + e.toString();
            }
            return stErrMsg;
        }
    
});

Leave a comment

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