Location Default customisation- sales order

During Sales order creation, check whether the “Location Override”(custom field) is checked in the corresponding customer record. If so, we need to get the customer record location value from the “Customer Default Location“ field and update the location on the item line(only for the kit, inventory, and assembly items) to that value. The updation only happens in the sales order creation.

UserEventScript

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/****************************************************************************
 * Macrofin Developers 
 * MAC-157 Plenish - Location Default customisation
 * **************************************************************************
 * Date: 21/05/2020
 * 
 * Script Description :  The script will check whether the “Location Override” is checked in the corresponding customer record. If so, the script will get the location value from the “Customer Default Location“ field and update the location on the item line to that value.
 * Date created : 21 May 2020
 *
 * REVISION HISTORY
 * 
 * Created By : Macrofin Developers
 *
 ****************************************************************************/
define(['N/record', 'N/search'],

    function(record, search) {
        function afterSubmit(scriptContext) {
            try {
                //Work on create context
                if (scriptContext.type == 'create') {
                    var RecordId = scriptContext.newRecord.id;
                    //Loads SO customer
                    var currentRec = record.load({
                        type: record.Type.SALES_ORDER,
                        id: RecordId
                    });
                    var CustomerId = currentRec.getValue({
                        fieldId: "entity"
                    });
                    log.debug("CustomerId", CustomerId);
                    //Get values from customer fields
                    var CustomerValues = search.lookupFields({
                        type: search.Type.CUSTOMER,
                        id: CustomerId,
                        columns: ['custentity_mf_jj_location_override', 'custentity_mf_jj_customer_deflt_location']
                    });
                    //Customer Default override and default location
                    var CustomerLocOverride = CustomerValues.custentity_mf_jj_location_override;
                    var CustomerDefaultLoc = CustomerValues.custentity_mf_jj_customer_deflt_location;
                    log.debug("CustomerDefaultLoc", CustomerDefaultLoc.length);
                    if (CustomerDefaultLoc.length == 1) {
                        var DefaultLocValue = CustomerDefaultLoc[0].value;
                    } else {
                        var DefaultLocValue = '';
                    }
                    log.debug("CustomerLocOverride", CustomerLocOverride);
                    log.debug("DefaultLocValue", DefaultLocValue);
                    if (CustomerLocOverride == true && DefaultLocValue != '') {
                        log.debug("Entered loop");
                        var itemLineCount = currentRec.getLineCount({
                            sublistId: 'item'
                        });
                        for (var i = 0; i < itemLineCount; i++) {
                            var ItemType = currentRec.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'itemtype',
                                line: i
                            });
                            if (ItemType == "Assembly" || ItemType == "InvtPart" || ItemType == "Kit") {
                                //Sets new location for inventory and assembly items
                                currentRec.setSublistValue({
                                    sublistId: 'item',
                                    fieldId: 'location',
                                    line: i,
                                    value: DefaultLocValue
                                });
                            }
                        }
                    }
                    currentRec.save();
                }

            } catch (e) {
                log.debug("ERROR@AFTERSUBMIT", e);
                log.error("ERROR@AFTERSUBMIT", e);
            }

        }
        return {
            afterSubmit: afterSubmit
        };

    });

Leave a comment

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