User Event Script to update the main location and line level location in a sales order.

User event script

 const beforeSubmit = (scriptContext) => {
            try {
                let newRecord = scriptContext.newRecord;
                if (scriptContext.type == 'create') {
                    let itemObject = {};
                    let lineCount = newRecord.getLineCount({
                        sublistId: 'item'
                    });
                    log.debug("lineCount", lineCount);
                    for (let i = 0; i < lineCount; i++) {
                        let item = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            line: i
                        });
                        let qty = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            line: i
                        });
                        if (!itemObject[item]) {
                            itemObject[item] = {};
                            itemObject[item]['qty'] = qty;
                        }
                        else {


                            itemObject[item]['qty'] += qty;
                        }


                    }
                    log.debug("itemObject", itemObject);
                    let result = itemSearch(itemObject);
                    let mainLocation = checkLocations(result);
                    log.debug("mainLocation", mainLocation);
                    if (mainLocation.flag && mainLocation.location) {
                        newRecord.setValue({
                            fieldId: 'location',
                            value: mainLocation.location
                        });
                    }
                    else if (!mainLocation.flag) {
                        newRecord.setValue({
                            fieldId: 'location',
                            value: " "
                        });
                    }
                    for (let i = 0; i < lineCount; i++) {
                        let item = newRecord.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            line: i
                        });
                        newRecord.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'location',
                            line: i,
                            value: result[item]['loc']
                        })


                    }


                }
            }
            catch (Err) {
                log.error("Error @ beforeSubmit", Err);
            }



        }

function itemSearch(obj) {
            try {
                let itemIds = Object.keys(obj);


                let itemSearch = search.create({
                    type: search.Type.ITEM,
                    filters: [
                        ['internalid', 'anyof', itemIds],
                        'AND',
                        ["locationquantityavailable", "greaterthan", "0"]
                    ],
                    columns: [
                        search.createColumn({ name: 'internalid' }),
                        search.createColumn({ name: 'locationquantityavailable' }),
                        search.createColumn({ name: 'inventorylocation' }),
                        search.createColumn({ name: 'preferredlocation' })
                    ]
                });


                itemSearch.run().each(function (result) {


                    let itemId = result.getValue({ name: 'internalid' });


                    let locationId = result.getValue({ name: 'inventorylocation' });


                    let locationAvailability = parseFloat(result.getValue({ name: 'locationquantityavailable' }));


                    let preferredLocation = result.getValue({ name: 'preferredlocation' });



                    if (obj[itemId]['qty'] < locationAvailability) {


                        if (obj[itemId]['loc'] && locationId == preferredLocation) {


                            obj[itemId]['loc'] = preferredLocation;
                        }
                        else {
                            obj[itemId]['loc'] = locationId;
                        }


                    }


                    return true;


                });
                log.debug("obj", obj);
                return obj;
            }
            catch (Err) {
                log.error("Error @ itemSearch", Err);
            }


        }
        function checkLocations(obj) {
            try {
                // Extract an array of all locations in the object
                const locations = Object.keys(obj)
                    .filter(key => obj[key].hasOwnProperty('loc'))
                    .map(key => obj[key].loc);


                // Check if all locations are the same
                const allSameLocation = locations.every(loc => loc === locations[0]);


                if (allSameLocation) {
                    log.debug("allSameLocation", allSameLocation);
                    log.debug("locations[0]", locations[0]);
                    return { flag: true, location: locations[0] };
                } else {
                    log.debug("In false flag");
                    return { flag: false };
                }
            }
            catch (Err) {
                log.error("Error @ checkLocations", Err);
            }


        }

Leave a comment

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