Add Button and Update the sub customers.

Jira Code: BTN-621

Create a button on the customer record. On that button click, update the group pricing and item pricing of the Sub Customers for that respective Customer.

    /**
     * @NApiVersion 2.x
     * @NScriptType UserEventScript
     * @NModuleScope SameAccount
     */
    /*******************************************************************************
     * BTN-621  UE Button For Customer Record.
     * **************************************************************************
     * 
     * Date: 18-04-2019
     * 
     * Author: Jobin & Jismi IT Services LLP
     * 
     *****************************************************************************
     **/
    define(['N/file', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget'],

        function(file, url, search, runtime, record, https, serverWidget) {
            var main = {
                beforeLoad: function(scriptContext) {
                    if (scriptContext.type == "edit"||scriptContext.type == "view") {
                        scriptContext.form.addButton({
                            id: 'custpage_buttons',
                            label: 'Copy Customer Pricing',
                            functionName: 'copyPricing'
                        });
                    }
                    scriptContext.form.clientScriptFileId = '1677940';
                }
            };
            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;

        });
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 *BTN-621  Customer Pricing 
 * **************************************************************************
 * 
 * Date: 03-07-2018
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 *****************************************************************************
 **/
define(['N/search', 'N/https', 'N/record', 'N/currentRecord', 'N/url'],

    function(search, https, record, currentRecord, url) {
        var flag = false;
        var main = {
            pageInit: function() {
                // body...
            },
            copyPricing: function() {
                var records = currentRecord.get().id;
                console.log("records",records)
                var customerRecord = record.load({
                    type: "customer",
                    id: records
                });
                var categoryId = customerRecord.getValue({
                    fieldId: 'custentity28'
                });
                var pricingDetails = main.getPricing(customerRecord,records);
                console.log("pricingDetails",pricingDetails)

                var response = https.post({
                    url: 'https://4215023-sb1.app.netsuite.com/app/site/hosting/scriptlet.nl?script=1566&deploy=1',
                    body: JSON.stringify(pricingDetails)
                });
            },
            getPricing: function(records,recordid) {
                var lines = records.getLineCount({ sublistId: "grouppricing" });
                console.log(lines);
                var pricingDetails = {};
                pricingDetails.id=recordid;
                var groupArray = [];
                /*getting Sublist values */
                for (var i = 0; i < lines; i++) {
                    var tempObj = {};
                    tempObj.group = records.getSublistValue({
                        sublistId: 'grouppricing',
                        fieldId: 'group',
                        line: i
                    });
                    tempObj.level = records.getSublistValue({
                        sublistId: 'grouppricing',
                        fieldId: 'level',
                        line: i
                    });
                    groupArray.push(tempObj);
                }
                pricingDetails.grouppricing = groupArray;
                var itemArray = [];
                /*getting Sublist values */
                var lines = records.getLineCount({ sublistId: "itempricing" });

                for (var i = 0; i < lines; i++) {
                    var tempObj = {};
                    tempObj.item = records.getSublistValue({
                        sublistId: 'itempricing',
                        fieldId: 'item',
                        line: i
                    });
                    tempObj.level = records.getSublistValue({
                        sublistId: 'itempricing',
                        fieldId: 'level',
                        line: i
                    });
                    tempObj.currency = records.getSublistValue({
                        sublistId: 'itempricing',
                        fieldId: 'currency',
                        line: i
                    });
                    tempObj.price = records.getSublistValue({
                        sublistId: 'itempricing',
                        fieldId: 'price',
                        line: i
                    });
                    itemArray.push(tempObj);
                }
                pricingDetails.itemArray = itemArray;

                return pricingDetails;
            }

        };

        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) {
                    console.error(e);
                }
            }
        };


        return main
    });
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
/**

* Script Description

* This suitelet to update the subcustomeres and call map reduce script

*/

/*******************************************************************************
 * 
 *  
 * NetSuite Name :BTN-621 Add Button
 * 
 * *****************************************************************************
 * 
 *  
 * $Author: Jobin & Jismi IT Services LLP $
 * 
 * DESCRIPTION
 * 
 * 
 * Date Created :19th April 2019
 * 
 * REVISION HISTORY Update:
 * 
 * 
 * 
 ******************************************************************************/
define(['N/file', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/encode', 'N/ui/serverWidget', 'N/task'],
    function (file, search, runtime, record, https, encode, serverWidget, task) {
        var main = {
            onRequest: function (scriptContext) {

                var parameters = JSON.parse(scriptContext.request.body)
                log.debug("scriptContext", parameters.id)
                var customerSearchObj = search.create({
                    type: "customer",
                    filters:
                        [
                            ["parentcustomer.internalid", "anyof", parameters.id]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "entityid",
                                sort: search.Sort.ASC,
                                label: "ID"
                            }),
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                var searchResultCount = customerSearchObj.runPaged().count;
                log.debug("customerSearchObj result count", searchResultCount);
                if(searchResultCount>65){
                    var scheduleScrptTask = task.create({
                        taskType: task.TaskType.MAP_REDUCE,
                        scriptId: "customscript_jj_mu_update_child",
                        deploymentId: 'customdeploy_jj_mu_update_child',
                        params: {
                            custscript_mu_data:scriptContext.request.body
                        }
                    });
                    scheduleScrptTask.submit();
                }else{
                var searchResult = customerSearchObj.run().getRange(0, 1000);
                for (var i = 0; i < searchResult.length; i++) {
                    var subcustomerid = searchResult[i].getValue({
                        name: "internalid",
                        label: "Internal ID"
                    });
                    var subcustomer = record.load({
                        type: "customer",
                        id: subcustomerid,
                        isDynamic: false
                    });
                    var grouppricing = parameters.grouppricing
                    for (var k = 0; k < grouppricing.length; k++) {
                        subcustomer.setSublistValue({
                            sublistId: 'grouppricing',
                            fieldId: 'group',
                            line: k,
                            value: grouppricing[k].group
                        });
                        subcustomer.setSublistValue({
                            sublistId: 'grouppricing',
                            fieldId: 'level',
                            line: k,
                            value: grouppricing[k].level
                        });
                    }
                    var itemArray = parameters.itemArray
                    log.debug("itemArray", itemArray.length)
                    for (var j = 0; j < itemArray.length; j++) {
                        subcustomer.setSublistValue({
                            sublistId: 'itempricing',
                            fieldId: 'item',
                            line: j,
                            value: itemArray[j].item
                        });
                        subcustomer.setSublistValue({
                            sublistId: 'itempricing',
                            fieldId: 'level',
                            line: j,
                            value: itemArray[j].level
                        });
                        subcustomer.setSublistValue({
                            sublistId: 'itempricing',
                            fieldId: 'currency',
                            line: j,
                            value: itemArray[j].currency
                        });
                        subcustomer.setSublistValue({
                            sublistId: 'itempricing',
                            fieldId: 'price',
                            line: j,
                            value: itemArray[j].price
                        });
                    }
                    var custId=subcustomer.save()
                    log.debug("custId",custId)
                }
            }
        }
        }
        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 *