Client script for pagination and other validation on a custom page developed by suitelet



define(['N/record', 'N/currentRecord', 'N/search', 'N/https', 'N/url', 'N/ui/message'],

    function(record, currentRecord, search, https, url, message ) {


        /**
         *
         * @param pageId
         */
        function getSuiteletPage(pageId) {
            try{

                document.location = url.resolveScript({
                    scriptId : 'customscript_jj_sl_createpage_marl_927',
                    deploymentId : 'customdeploy_jj_sl_createpage_marl_927',
                    params : {
                        'page' : pageId
                    }
                });
            }catch(error){
                log.error("Error @getSuiteletPage",error);
            }
        }
        //function to group
        function groupArrayOfObjects(list, key) {
            try {

                return list.reduce(function (rv, x) {
                    (rv[x[key]] = rv[x[key]] || []).push(x);

                    return rv;
                }, {});
            }catch (e) {
                log.debug("error @ groupArrayOfObjects", e)
            }
        }


        //function to update ship date
        function updateShipdate() {

            var itemandSOArray = [];
            var concatArr = [];
            var count = 0;
            var soSublist = currentRecord.get();
            //get shipdate
            var isshipdate = soSublist.getValue({
                fieldId: 'custpage_date_field',
            })
            //get page
            var pageId= soSublist.getValue({
                fieldId:'custpage_pageid'
            });

            // convert ship date to mm/dd/yyyy format
            var shipdate= new Date(isshipdate);
            var dd = shipdate.getDate();
            var mm = shipdate.getMonth() + 1;
            var yyyy = shipdate.getFullYear();
            var newShipdate= mm + "/" + dd  + "/"+ yyyy;
            //  console.log("newShipdate", newShipdate)
            if (isshipdate) {
                var lines = soSublist.getLineCount({
                    sublistId: "custpage_item_lines"
                })
                for (var i = 0; i < lines; i++) {
                    var chkboxValue = soSublist.getSublistValue({
                        sublistId: 'custpage_item_lines',
                        fieldId: 'custpage_chk',
                        line: i
                    });
                    if (chkboxValue == true) {

                        var soId = soSublist.getSublistValue({
                            sublistId: 'custpage_item_lines',
                            fieldId: 'custpage_docnum',
                            line: i
                        });

                        var soObj = {};
                        soObj.salesOrderId = soSublist.getSublistValue({
                            sublistId: 'custpage_item_lines',
                            fieldId: 'custpage_intid',
                            line: i
                        });
                        soObj.soItemName = soSublist.getSublistValue({
                            sublistId: 'custpage_item_lines',
                            fieldId: 'custpage_item_name',
                            line: i
                        });
                        soObj.uniqueId = soSublist.getSublistValue({
                            sublistId: 'custpage_item_lines',
                            fieldId: 'custpage_unique_key',
                            line: i
                        })
                        soObj.shipDate = newShipdate
                        itemandSOArray.push(soObj);
                    }
                }
                console.log("itemandSOArray", itemandSOArray)
                var groupedArray = groupArrayOfObjects(itemandSOArray, "salesOrderId");
                console.log("grp array", groupedArray)
                var groupedArrayLength = Object.keys(groupedArray).length;
                var entries = Object.entries(groupedArray)
                console.log("entries", entries)
                var newArray = [];

                for (var k = 0; k < groupedArrayLength; k++) {
                    //console.log("entered")
                    var keyElements = Object.keys(groupedArray)[k]
                    var valueElements = Object.values(groupedArray)[k]
                    //console.log("valueElements", keyElements)
                    var object = {};
                    var firstKey = keyElements;
                    var firstValue = valueElements;
                    object[firstKey] = firstValue;
                    console.log("object", object)
                    //Object.assign(object, { [firstKey]: firstValue });
                    newArray.push(object)
                }

                console.log("newArray", newArray)
                var get_url = url.resolveScript({
                    scriptId: 'customscript_jj_sl_shipdtupdate_marl_929',
                    deploymentId: 'customdeploy_jj_sl_shipdtupdate_marl_929',
                    returnExternalUrl: false
                });

            if(newArray.length>0){
                if (newArray.length > 20) {
                    alert("Please reduce the sales order selection to 20")
                }else{
                    // showing message
                    var waitMsg = message.create({
                        title: "Please wait...",
                        message: "Ship date update script is running, Page will auto-refresh once update is completed.",
                        type: message.Type.WARNING
                    });

                    // will disappear after 5s
                    waitMsg.show({
                        duration: 100000
                    });

                    if (jQuery('#tdbody_custpage_update_shipdate').length == 1) {
                        jQuery('#tdbody_custpage_update_shipdate').css('pointer-events', 'none');
                        jQuery('#tdbody_secondarycustpage_update_shipdate').css('pointer-events', 'none');
                        jQuery('#tdbody_secondarycustpage_update_shipdate').css('opacity', '0.6');
                        jQuery('#tdbody_custpage_update_shipdate').css('opacity', '0.6');

                        jQuery("#custpage_load_img_new").css("display", "block");
                    }



                        for (var index = 0; index < newArray.length; index++) {

                            https.post.promise({
                                url: get_url,
                                headers: {
                                    'Content-Type': 'application/json'
                                },
                                body: JSON.stringify(newArray[index])
                            }).then(function (response) {

                                var resObj = response.body;
                                var passObj = JSON.parse(resObj);
                                if(passObj == false){
                                    concatArr.push(passObj)
                                    var myMsg = message.create({
                                        title: "Congratulations",
                                        message: "Successfully completed the ship date updating on Sales orders, and Sales orders with the following internal ids" + concatArr+ "are not updated.",
                                        type: message.Type.CONFIRMATION
                                    });
                                }
                                if (response.body) {
                                    count++;
                                }
                                if (count == newArray.length) {

                                    setTimeout(waitMsg.hide());
                                    var myMsg = message.create({
                                        title: "Congratulations",
                                        message: "Successfully completed the ship date update on Sales orders.",
                                        type: message.Type.CONFIRMATION
                                    });


                                    // will disappear after 5s
                                    myMsg.show({
                                        duration: 10000
                                    });
                                    window.onbeforeunload = function () { };
                                    location.reload();

                                }


                            }).catch(function onRejected(reason) {
                                console.log(reason);
                            });
                        }
                }
            }
            }


            else{
                alert("Please enter a value for Ship Date")
            }
        }

        function fieldChanged(scriptContext) {
            try{
                if (scriptContext.fieldId === 'custpage_pageid') {
                    var pageId = scriptContext.currentRecord.getValue({
                        fieldId : 'custpage_pageid'
                    });
                    pageId = parseInt(pageId.split('_')[1]);
                    if (window.onbeforeunload) {
                        window.onbeforeunload = function() {
                            null;
                        };
                    };
                    document.location = url.resolveScript({
                        scriptId : 'customscript_jj_sl_createpage_marl_927',
                        deploymentId : 'customdeploy_jj_sl_createpage_marl_927',
                        params : {
                            'page' : pageId
                        }
                    });

                }
                if(scriptContext.fieldId === 'custpage_salesorder_field'){
                    var soId = scriptContext.currentRecord.getValue({
                        fieldId : 'custpage_salesorder_field'
                    });
                    if (window.onbeforeunload) {
                        window.onbeforeunload = function() {
                            null;
                        };
                    };
                    document.location = url.resolveScript({
                        scriptId : 'customscript_jj_sl_createpage_marl_927',
                        deploymentId : 'customdeploy_jj_sl_createpage_marl_927',
                        params : {
                            'soid' : soId
                        }
                    });
                }
                if(scriptContext.fieldId === 'custpage_po_field'){
                    var poNumber = scriptContext.currentRecord.getValue({
                        fieldId : 'custpage_po_field'
                    });
                    if (window.onbeforeunload) {
                        window.onbeforeunload = function() {
                            null;
                        };
                    };
                    document.location = url.resolveScript({
                        scriptId : 'customscript_jj_sl_createpage_marl_927',
                        deploymentId : 'customdeploy_jj_sl_createpage_marl_927',
                        params : {
                            'ponum' : poNumber
                        }
                    });
                }



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

        return {

            fieldChanged: fieldChanged,
            updateShipdate: updateShipdate,
            getSuiteletPage: getSuiteletPage,

        };

    });

Leave a comment

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