Script to get the preferred bin and set to a custom column

This script reads and sets the preferred bin to custom column Bin# for the deployed transactions
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
/***
 * FLTW-68 Bin Population script Optimization
 */
define(['N/record', 'N/search'],
    function (record, search) {

        function afterSubmit(context) {
            try {
                if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT)
                    // works for create and edit only
                    return;
                var soRecord = context.newRecord;
                var so_id = soRecord.id;
                var so_type = soRecord.type;
                var so_location = soRecord.getValue({
                    fieldId: 'location'
                });

                var numLines = soRecord.getLineCount({
                    sublistId: 'item'
                });
                log.debug("SO "+so_id,"numLines "+numLines);

                if (numLines > 0) {
                    var bin_locArray = getPreferedBinForLocation(so_id, so_location);

                    try {

                        var objRecord = record.load({
                            type: so_type,
                            id: so_id,
                            isDynamic: true,
                        });

                        for (var i = 0; i < numLines; i++) {

                            var itemid = objRecord.getSublistValue({
                                sublistId: 'item',
                                fieldId: 'item',
                                line: i
                            });
                            if (itemid == '-2' || itemid == '-3' || itemid == '-4' || itemid == '-5' || itemid == '-6') {//
                            } else {
                                var linenum = objRecord.selectLine({
                                    sublistId: 'item',
                                    line: i
                                });
                                var prefBin = findElement(bin_locArray, itemid);
                                //log.debug("prefBin " + prefBin, "itemid " + itemid);
                                objRecord.setCurrentSublistValue({
                                    sublistId: 'item',
                                    fieldId: 'custcol_czo_preferred_bin',
                                    value: prefBin,
                                    ignoreFieldChange: true
                                });
                                objRecord.commitLine({
                                    sublistId: 'item'
                                });
                            }


                        }
                        var newrecid = objRecord.save();
                        log.debug("Success", newrecid);
                    } catch (error) {
                        log.error("E@saveRec", error);
                    }

                } else {
                    log.debug("ZERO SO " + so_id, "numLines " + numLines);
                }
            } catch (e) {
                log.error("e@MAIN", e.name);
            }

        }

        return {

            afterSubmit: afterSubmit
        };

        // function to read value from JSON obj array
        function findElement(arr, propValue) {
            for (var i = 0; i < arr.length; i++)

                if (arr[i]["item"] == propValue)
                    return arr[i]["binnumber"];

            // will return undefined if not found; you could return a default instead
        }

        // search returns bin number for each location
        function getPreferedBinForLocation(so_id, so_location) {
            try {
                var results_Array = [];

                var transactionSearchObj = search.create({
                    type: "transaction",
                    filters:
                        [
                            ["internalidnumber", "equalto", so_id],
                            "AND",
                            ["item.location", "anyof", so_location],
                            "AND",
                            ["item.preferredbin", "is", "T"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "item", label: "Item"}),
                            search.createColumn({
                                name: "location",
                                join: "item",
                                label: "Location"
                            }),
                            search.createColumn({
                                name: "binnumber",
                                join: "item",
                                label: "Bin Number"
                            }),

                        ]
                });
                var searchResultCount = transactionSearchObj.runPaged().count;
               // log.debug("transactionSearchObj result count", searchResultCount);
                transactionSearchObj.run().each(function (result) {
                    // .run().each has a limit of 4,000 results
                    var item = result.getValue({
                        name: "item"
                    });
                    var binnumber = result.getValue({
                        name: "binnumber",
                        join: "item",
                        label: "Bin Number"
                    });

                    results_Array.push({
                        "item": item,
                        "binnumber": binnumber
                    });

                    return true;
                });
                return results_Array;
            } catch (e) {
                log.error("e@ getprefbin", e);
            }
        }
    });

Leave a comment

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