PM Review for MPCR in employee center

Requirement

Need to create a custom page to list all the mpcr that are open status and the current user is the corresponding project manager (Body or Line). The user is able to select the mpcr and click submit button and the corresponding PM review checkbox will be checked and the corresponding custom fields : Reviewed By and Review Date will update. The Suitelet will be added in the employee center roles only and add it as a link in the employee center dashboard.

Solution

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */

define(['N/currentRecord', 'N/record', 'N/render', 'N/runtime', 'N/search', 'N/ui/serverWidget', 'N/url'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{render} render
     * @param{runtime} runtime
     * @param{search} search
     * @param{serverWidget} serverWidget,
     * @param{url} url
     */
    (currentRecord, record, render, runtime, search, serverWidget, url) => {
        /**
         * Defines the Suitelet script trigger point.
         * @param {Object} scriptContext
         * @param {ServerRequest} scriptContext.request - Incoming request
         * @param {ServerResponse} scriptContext.response - Suitelet response
         * @since 2015.2
         */

        //Draft invoice search created  to list all the mpcr that are open status and the current user is the corresponding project manager (Body or Line).
        //The Suitelet will be added in the employee center roles only and add it as a link in the employee center dashboard.
        function draftInvoiceSearch() {
            var customrecord_jj_invoice_mpcrSearchObj = search.create({
                type: "customrecord_jj_invoice_mpcr",
                filters:
                    [
                        ["custrecord_jj_status", "anyof", "1"],
                        "AND",
                        ["formulanumeric: CASE WHEN {user} = {custrecord_jj_protect_name.projectmanager.id}THEN 1 ELSE 0 END", "equalto", "1"],
                        "OR",
                        [["formulanumeric: CASE WHEN {user} = {custrecord_jj_protect_name.jobresource.id}THEN 1 ELSE 0 END", "equalto", "1"],
                            "AND",
                            ["custrecord_jj_protect_name.jobresourcerole", "anyof", "-2"]],
                        "AND",
                        ["custrecord_jj_pm_reviewed_aq_3335", "is", "F"],
                       
                    ],
                columns:
                    [
                        search.createColumn({
                            name: "name",
                            sort: search.Sort.ASC,
                            label: "ID"
                        }),
                        search.createColumn({name: "internalid", label: "Internal ID"}),
                        search.createColumn({name: "scriptid", label: "Script ID"}),
                        search.createColumn({name: "custrecord_jj_date", label: "Date"}),
                        search.createColumn({name: "custrecord_jj_bill_expense_total", label: "Expense Total"}),
                        search.createColumn({name: "custrecord_jj_billable_item_total", label: "Item Total"}),
                        search.createColumn({name: "custrecord_jj_billable_time_total", label: "Time/Fees Total"}),
                        search.createColumn({name: "custrecord_jj_tax_total", label: "Tax Total"}),
                        search.createColumn({name: "custrecord_jj_total", label: "Total"}),
                        search.createColumn({name: "custrecord_jj_due_date", label: "Due date"}),
                        search.createColumn({name: "custrecord_jj_status", label: "Status"}),
                        search.createColumn({name: "custrecord_jj_salesinvoice_mpcr", label: "Sales Invoice Id"}),
                        search.createColumn({name: "custrecord_jj_posting_period", label: "Posting Period"}),
                        search.createColumn({name: "custrecord_jj_invoice_memo", label: "Memo"}),

                        search.createColumn({name: "custrecord_jj_invoice_subsidiary", label: "Subsidiary"}),
                        search.createColumn({name: "custrecord_jj_currency", label: "Currency"}),
                        search.createColumn({
                            name: "jobresource",
                            join: "CUSTRECORD_JJ_PROTECT_NAME",
                            label: "Project Resource"
                        }),
                        search.createColumn({name: "custrecord_jj_project_manager", label: "Project Manager"}),
                        search.createColumn({
                            name: "jobresourcerole",
                            join: "CUSTRECORD_JJ_PROTECT_NAME",
                            label: "Project Resource Role"
                        }),
                        search.createColumn({
                            name: "internalid",
                            join: "user",
                            label: "Internal ID"
                        }),
                        search.createColumn({name: "custrecord_jj_protect_name", label: "Project"}),
                        search.createColumn({name: "custrecord_jj_cr_client_name", label: "Client"}),
                        search.createColumn({
                            name: "formulatext",
                            formula: "ltrim(regexp_substr({custrecord_jj_posting_period},'[^:]*$'))",
                            label: "Formula (Text)"
                        })
                    ]
            });
            var searcCount = customrecord_jj_invoice_mpcrSearchObj.runPaged().count;
            log.debug('Search real count', searcCount)
            var searchResultObj = customrecord_jj_invoice_mpcrSearchObj.run().getRange({
                start: 0,
                end: 1000
            })
            log.debug('SearchresultObj', searchResultObj)
            return searchResultObj;
        }


        const onRequest = (scriptContext) => {
            try {
                if (scriptContext.request.method === 'GET') {
                    //Check whether it is the employee center role
                    var centerType = runtime.getCurrentUser().roleCenter
                    log.debug('Role...........', centerType)
                    //Below code will execute only if it a employee center role.
                     if (centerType === 'EMPLOYEE') {
                    var form = serverWidget.createForm({title: 'DRAFT INVOICE PM REVIEW'});
                    form.clientScriptFileId = 179495;
                    //Sublist is added to the form
                    var sublistDet = form.addSublist({
                        id: 'custpage_det',
                        type: serverWidget.SublistType.LIST,
                        label: 'Draft Invoice Details'
                    });

                    //Mark all and UnmarkAll button is added to the suitelet page.
                    sublistDet.addMarkAllButtons();

                    //Check box is added for the selection of particular draft invoice.
                    var selectCheck = sublistDet.addField({
                        id: 'custpage_checkid',
                        type: serverWidget.FieldType.CHECKBOX,
                        label: 'SELECT'
                    })

                    //Id and User is added and is hidden for the future use of these field values.
                    var hideId = sublistDet.addField({
                        id: 'custpage_hideid',
                        label: 'Id',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT,

                    });
                    var hideUser = sublistDet.addField({
                        id: 'custpage_hideuser',
                        label: 'User',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT,

                    });

                    //Id and User fields set with fielddisplaytype as hidden
                    hideId.updateDisplayType({

                        displayType: serverWidget.FieldDisplayType.HIDDEN

                    });
                    hideUser.updateDisplayType({

                        displayType: serverWidget.FieldDisplayType.HIDDEN

                    });

                    //Fieds for displaying different Details of the draft invoice is added to the page
                    var name = sublistDet.addField({
                        id: 'custpage_name',
                        label: 'ID',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });

                    var date = sublistDet.addField({
                        id: 'custpage_date',
                        label: 'Date',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var feesTotal = sublistDet.addField({
                        id: 'custpage_feestotal',
                        label: 'TIME/FEES TOTAL',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var expenseTotal = sublistDet.addField({
                        id: 'custpage_expensetotal',
                        label: 'EXPENSE TOTAL',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var itemTotal = sublistDet.addField({
                        id: 'custpage_itemtotal',
                        label: 'ITEM TOTAL',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var taxTotal = sublistDet.addField({
                        id: 'custpage_taxtotal',
                        label: 'TAX TOTAL',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });

                    var total = sublistDet.addField({
                        id: 'custpage_total',
                        label: 'Total',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var currency = sublistDet.addField({
                        id: 'custpage_currency',
                        label: 'CURRENCY',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var dueDate = sublistDet.addField({
                        id: 'custpage_duedate',
                        label: 'DUE DATE',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var status = sublistDet.addField({
                        id: 'custpage_status',
                        label: 'STATUS',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var postingPeriod = sublistDet.addField({
                        id: 'custpage_postperiod',
                        label: 'POSTING PERIOD',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });
                    var projectId = sublistDet.addField({
                        id: 'custpage_projectid',
                        label: 'PROJECT ID',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });

                    var client = sublistDet.addField({
                        id: 'custpage_client',
                        label: 'CLIENT',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });

                    var subsidiary = sublistDet.addField({
                        id: 'custpage_subsidiary',
                        label: 'SUBSIDIARY',
                        type: serverWidget.FieldType.TEXT,
                        align: serverWidget.LayoutJustification.RIGHT
                    });

                    //Function named draftInvoiceSearch() which in turn invoke draft invoice search is called here.
                    var searchResult = draftInvoiceSearch();
                    log.debug('SearchResult', searchResult)

                 
                    form.addSubmitButton({
                        id: 'custpage_mark_review',
                        label: 'SUBMIT',
                     
                    })

                    //Count of search result is taken
                    log.debug('Count', searchResult.length)


                    //Looping through the searchresult to set the values to corresponding fields in the page.
                    for (var i = 0; i < searchResult.length; i++) {

                        sublistDet.setSublistValue({
                            id: 'custpage_name',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "name",
                                    sort: search.Sort.ASC,
                                    label: "ID"
                                }
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_hideid',
                            line: i,
                            value: (searchResult[i].getValue({name: "internalid", label: "Internal ID"}
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_hideuser',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "internalid",
                                    join: "user",
                                    label: "Internal ID"
                                }
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_date',
                            line: i,
                            value: (searchResult[i].getValue({name: "custrecord_jj_date", label: "Date"}
                            )) || " "
                        });

                        sublistDet.setSublistValue({
                            id: 'custpage_feestotal',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "custrecord_jj_billable_time_total",
                                    label: "Time/Fees Total"
                                }
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_expensetotal',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "custrecord_jj_bill_expense_total",
                                    label: "Expense Total"
                                }
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_itemtotal',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "custrecord_jj_billable_item_total",
                                    label: "Item Total"
                                }
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_taxtotal',
                            line: i,
                            value: (searchResult[i].getValue({name: "custrecord_jj_tax_total", label: "Tax Total"}
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_total',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "custrecord_jj_billable_item_total",
                                    label: "Item Total"
                                }
                            )) || " "
                        });

                        sublistDet.setSublistValue({
                            id: 'custpage_duedate',
                            line: i,
                            value: (searchResult[i].getValue({name: "custrecord_jj_due_date", label: "Due date"}
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_status',
                            line: i,
                            value: (searchResult[i].getText({name: "custrecord_jj_status", label: "Status"}
                            )) || " "
                        });
                        var post = searchResult[i].getText({
                            name: "custrecord_jj_posting_period",
                            label: "Posting Period"
                        });

                        sublistDet.setSublistValue({
                            id: 'custpage_postperiod',
                            line: i,
                            value: (searchResult[i].getValue({
                                    name: "formulatext",
                                    formula: "ltrim(regexp_substr({custrecord_jj_posting_period},'[^:]*$'))",
                                    label: "Formula (Text)"
                                }
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_projectid',
                            line: i,
                            value: searchResult[i].getText({name: "custrecord_jj_protect_name", label: "Project"}
                            ) || ""
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_client',
                            line: i,
                            value: (searchResult[i].getText({name: "custrecord_jj_cr_client_name", label: "Client"}
                            )) || " "
                        });

                        sublistDet.setSublistValue({
                            id: 'custpage_currency',
                            line: i,
                            value: (searchResult[i].getText({name: "custrecord_jj_currency", label: "Currency"}
                            )) || " "
                        });

                        sublistDet.setSublistValue({
                            id: 'custpage_currency',
                            line: i,
                            value: (searchResult[i].getText({name: "custrecord_jj_currency", label: "Currency"}
                            )) || " "
                        });
                        sublistDet.setSublistValue({
                            id: 'custpage_subsidiary',
                            line: i,
                            value: (searchResult[i].getText({
                                    name: "custrecord_jj_invoice_subsidiary",
                                    label: "Subsidiary"
                                }
                            )) || " "
                        });

                    }

                    //Writing the form
                    scriptContext.response.writePage(form);
                    }
                } else if (scriptContext.request.method === 'POST') {



                    log.debug('scriptContext.response', scriptContext.response)
                    var response = scriptContext.response;

                    //Sublist count is taken
                   var sublistCount = scriptContext.request.getLineCount({group: "custpage_det"});
                    log.debug('Sublist count in post', sublistCount)

                    //Iterating through the sublist values
                    for (var j = 0; j < sublistCount; j++) {
                        //Checking wheather the select box is checked or not
                        var getStatus = scriptContext.request.getSublistValue({
                            group: 'custpage_det',
                            name: 'custpage_checkid',
                            line: j
                        });

                        //Get Id of the draft invoice
                        var getId = scriptContext.request.getSublistValue({
                            group: 'custpage_det',
                            name: 'custpage_hideid',
                            line: j
                        });

                        //Get current User
                        var getUser = scriptContext.request.getSublistValue({
                            group: 'custpage_det',
                            name: 'custpage_hideuser',
                            line: j
                        });


                        //If any of the checkbox is found checked, it is considered and corresponding draft invoice filed values will be populated.
                        if (getStatus === 'T') {

                            //Get current date and set it to the date format.
                            var today = new Date();
                            var year = today.getFullYear();
                            var month = today.getMonth() + 1;
                            var day = today.getDate()
                            var todayDate = day + "/" + month + "/" + year
                            log.debug('Today',today)
                            log.debug('year',year)
                            log.debug('month',month)
                            log.debug('day',day)
                            log.debug('today date',todayDate)
                            //Set the PM revied checkbox to true and Submit the date and  user to the fields in to the draft invoice record.
                            record.submitFields({
                                type: 'customrecord_jj_invoice_mpcr',
                                id: getId,
                                values: {
                                    custrecord_jj_pm_reviewed_aq_3335: true,
                                    custrecord_jj_reviewed_by_aq_3335: getUser,
                                    custrecord_jj_review_date_aq_3335: todayDate
                                },
                                options: {
                                    enableSourcing: false,
                                    ignoreMandatoryFields: true
                                }
                            });
                            var htmlCode = '<html><body><script type="text/javascript">window.history.back();</script></body></html>'
                            scriptContext.response.write(htmlCode);
                            // var url = '/app/site/hosting/scriptlet.nl?script=655&deploy=1'

                        }
                    }
                }
            } catch (e) {
                log.debug('Error @ onRequest', e)
            }
        }

        return {onRequest}

    });

Leave a comment

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