Passing a variable as a function parameter to a referenced Client Script

Requirements

User/customer wants to pass a value from their User Event script to a referenced Client Script so that it can be used for further processing within the Client Script. The Client Script will be triggered by a button that was created by the User Event script on beforeLoad.

Solution

A variable that contains the desired value will be passed as a function parameter in one of the functions of the referenced Client Script. The sample codes below will include scenarios for when passing a number, a string, or a URL parameter.

Step 1: Create a client script that will use the value to be passed and upload it to the file cabinet. 

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord', 'N/url'],
/**
 * @param{currentRecord} currentRecord
 * @param{url} url
 */
function(currentRecord, url) {
    
    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     */
    function pageInit(scriptContext)
    {
    }

    function customerAgreement(recordType)
    {
        try
        {

            let internalId = currentRecord.get().id;
            log.debug({title: "internalId", details: internalId});
            log.debug({title: "recordTypeCL", details: recordType});
            let currentUrl = url.resolveScript({
                scriptId: "customscript_jj_sl_agreement_doc",
                deploymentId: "customdeploy_jj_sl_agreement_doc",
                params: {internalIds: internalId, recordTypes:recordType, custBtnClick: 0}         
            })
            window.open(currentUrl + "&internalIds=" + internalId)
        }
        catch (e)
        {
            log.error({title: "error@customerPaymentReceipt", details: e});
        }
    }
    return {pageInit: pageInit,customerAgreement:customerAgreement};
});

Step 2: Create the User Event script that will reference the Client Script and pass the value to it. Deploy script to the intended record.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define([],
  
    () => {
        /**
         * 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
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) =>
        {
                if (scriptContext.type === 'view')
                {
                        let recordType = scriptContext.newRecord.getValue({fieldId: 'baserecordtype'});
                        scriptContext.form.addButton({
                                id: 'custpage_customer_agreement_document_button',
                                label: 'Agreement Document',
                                functionName: "customerAgreement('" + recordType + "')"
                        });
                        scriptContext.form.clientScriptModulePath=  './JJ CL AGREEMENT DOCUMNET JJIT-2942.js'; //client script for button action
                }
        }
        return {beforeLoad}
    });

Leave a comment

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