Set the location in item level of sales order

To set location in item level of Sales order with the body level location field value , if there is no line level sales order location. We can use UserEvent Script





/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

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

        const afterSubmit = (scriptContext) => {
try {
    if (scriptContext.newRecord.type == record.Type.SALES_ORDER) {

        var recordId = scriptContext.newRecord.id;        // to get current sales order ID
        

        var objRecord = record.load({                    // load the sales order using ID
            type: record.Type.SALES_ORDER,
            id: recordId,
            isDynamic: true,
        });
        
        var objRecordlocation = objRecord.getValue({
            fieldId: 'location'
        })
        log.debug('location', objRecordlocation);



            var itemCount = objRecord.getLineCount({
                sublistId: 'item'
            });
            for (var i = 0; i < itemCount; i++) {
                var itemName = objRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'item',
                    line: i
                });
                
                        objRecord.selectLine({
                        sublistId: 'item',
                        line: i
                    });
                    var itemlocation = objRecord.getCurrentSublistValue({      
                        sublistId: 'item',
                        fieldId: 'location',
                    })
                    // log.debug("itemlocation",itemlocation);

                    if (!itemlocation) {        // If item level location does not exist

                        objRecord.setCurrentSublistValue({        // set the item location
                            sublistId: 'item',
                            fieldId: 'location',
                            value: objRecordlocation,
                            ignoreFieldChange: true
                        });

                        objRecord.commitLine({
                            sublistId: 'item'
                        });

                    }


            }
        
        objRecord.save({                 
            enableSourcing: true,
            ignoreMandatoryFields: true
        });
    }
}
          catch(err){
              log.debug("error",err);
                    }
        }
        return {afterSubmit}

    });

Leave a comment

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