Create saved search from getting back orderd item in the sales order

This code will help to get backorder from the sales order using transaction search.

    /**
     * @NApiVersion 2.x
     * @NScriptType UserEventScript
     * @NModuleScope SameAccount
     */
    /*CORP 57 N0tify Ware house Managers */
    define(['N/file', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget', 'N/email'],

        function(file, url, search, runtime, record, https, serverWidget, email) {
            var main = {
                beforeSubmit: function(scriptContext) {
                    if (scriptContext.type == "create" || scriptContext.type == "edit") {
                    var itemarray = ["item.internalid", "anyof",item_array_detail];
        
                    var filter = [
                        ["type", "anyof", "SalesOrd"],
                        "AND",
                        ["status", "anyof", "SalesOrd:D", "SalesOrd:E", "SalesOrd:B", "SalesOrd:A"],
                        "AND",
                        ["mainline", "is", "F"],
                        "AND",
                        ["accounttype", "anyof", "Income"],
                        "AND",
                        ["formulanumeric: {quantity}-nvl({quantityshiprecv},0)-nvl({quantitycommitted},0)", "greaterthan", "0"],
                        "AND",
                        ["shipping", "is", "F"],
                        "AND",
                        ["taxline", "is", "F"],
                        "AND",
                        ["closed", "is", "F"],
                        "AND",
                        ["location","anyof",location],
                        "AND",
                        itemarray
                    ]
                    
                    var salesorderSearchObj = search.create({
                        type: "salesorder",
                        filters: [
                            filter
                        ],
                        columns: [
                            search.createColumn({
                                name: "entity",
                                sort: search.Sort.ASC,
                                label: "Name"
                            }),
                            search.createColumn({
                                name: "internalid",
                                join: "customer",
                                label: "Internal ID"
                            }),
                            search.createColumn({ name: "internalid", label: "Internal ID" }),
                            search.createColumn({ name: "tranid", label: "Document Number" }),
                            search.createColumn({ name: "salesrep", label: "Sales Rep" }),
                            search.createColumn({
                                name: "quantitybackordered",
                                join: "item",
                                label: "Back Ordered"
                            })
                        ]
                    });
                    var searchResultCount = salesorderSearchObj.runPaged().count;
                    log.debug("salesorderSearchObj result count", searchResultCount);
                    var salesorderarray = [];
                    salesorderSearchObj.run().each(function(result) {
                        var salesobj = {};
                        var back = result.getValue({ name: "quantitybackordered", join: "item" });
                        if (back > 0) {
                            salesobj.entity = result.getText("entity");
                            salesobj.custInt = result.getValue({ name: "internalid", join: "customer", label: "Internal ID" });
                            salesobj.internalid = result.getValue("internalid");
                            salesobj.traind = result.getValue("tranid");
                            salesobj.salesrep = result.getValue("salesrep");

                            salesorderarray.push(salesobj);
                        }

                        return true;
                    });
 }
            };
            for (var key in main) {
                if (typeof main[key] === 'function') {
                    main[key] = trycatch(main[key], key);
                }
            }

            function trycatch(myfunction, key) {
                return function() {
                    try {
                        return myfunction.apply(this, arguments);
                    } catch (e) {
                        log.debug("e in  " + key, e);
                    }
                }
            };
            return main;
        });

Leave a comment

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