User Event Script To Add Print Back Ordered Item Report Button In IF Record

Client requires a custom button to print back-ordered items as a PDF report.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/ui/serverWidget'],
    /**
 * @param{record} record
 * @param{serverWidget} serverWidget
 */
    (record, serverWidget) => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            try{
                let createdFrom = scriptContext.newRecord.getValue({fieldId: 'createdfrom'});
                if(scriptContext.type != "view" || !createdFrom)
                   return;
                let salesOrder= record.load({
                    type: record.Type.SALES_ORDER,
                    id: createdFrom
                })
                let length= salesOrder.getLineCount({sublistId: 'item'});
                let flag=0;
                for(let i=0;i<length;i++){
                    let backQuantity=salesOrder.getSublistValue({sublistId: 'item', fieldId: 'quantitybackordered', line: i});
                    if(backQuantity>0)
                      flag= flag+1;
                }
                if(flag <=0) return;
                scriptContext.form.clientScriptModulePath = "SuiteScripts/Jobin and Jismi/BILL_155 Print Backordered Report/Action/jj_cs_print_button_action_bill_155.js";
                scriptContext.form.addButton({
                    id: 'custpage_jj_print_report_button_bill_155',
                    label: 'Print Back-Ordered Report',
                    functionName:`printButton(${createdFrom})`
                });
            }
            catch(e){
                log.error("error@beforeLoad",e)
            }


        }


        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {


        }


        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {


        }


        return {beforeLoad, beforeSubmit, afterSubmit}


    });


Leave a comment

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