Update Fields on Current Record using a Custom Module Script and a User Event Script

Note:

This sample includes two scripts: a client script (clientDemo.js) which is called by a user event script that uses the custom module client script.

The following sample is a custom module client script named clientDemo.js. This script updates fields on the current record. After you upload the clientDemo.js script file to a NetSuite account, it can be called by other scripts.

Because clientDemo.js is a custom module script, it must manually load the N/currentRecord Samples method by naming it in the define statement. It must also retrieve a currentRecord.CurrentRecord object by using the currentRecord.get() method.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.x Global Objects.

          /**
 * @NApiVersion 2.1
 */
define(['N/currentRecord'], currentRecord => {
    return ({
        test_set_getValue: () => {
            // Get a reference to the currently active record
            let myRecord = currentRecord.get();
             
            // Set the value of a custom field
            myRecord.setValue({
                fieldId: 'custpage_textfield',
                value: 'Body value',
                ignoreFieldChange: true,
                forceSyncSourcing: true
            });
             
            // Retrieve the value that was set
            let actValue = myRecord.getValue({
                fieldId: 'custpage_textfield'
            });
             
            // Set the value of another custom field
            myRecord.setValue({
                fieldId: 'custpage_resultfield',
                value: actValue,
                ignoreFieldChange: true,
                forceSyncSourcing: true
            });
        },
         
        test_set_getCurrentSublistValue: () => {
            // Get a reference to the currently active record
            let myRecord = currentRecord.get();
             
            // Set the value of a custom sublist field
            myRecord.setCurrentSublistValue({
                sublistId: 'sitecategory',
                fieldId: 'custpage_subtextfield',
                value: 'Sublist Value',
                ignoreFieldChange: true,
                forceSyncSourcing: true
            });
             
            // Retrieve the value that was set
            let actValue2 = myRecord.getCurrentSublistValue({
                sublistId: 'sitecategory',
                fieldId: 'custpage_subtextfield'
            });
             
            // Set the value of another custom field
            myRecord.setValue({
                fieldId: 'custpage_sublist_resultfield',
                value: actValue2,
                ignoreFieldChange: true,
                forceSyncSourcing: true
            });
        }
    });
}); 

        

The following sample is a user event script deployed on a non-inventory item record. Before the record loads, the script updates the form used by the record to add new text fields, a sublist, and buttons that call the methods in a custom module client script (clientDemo.js). The buttons access the current record and set values for some of the form’s fields. This sample demonstrates how to customize a form, use a custom module client script, and see the new fields and buttons in action.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

          /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define([], () => {
    return {
        beforeLoad: params => {
            // Get a reference to the current form that is about to load
            let form = params.form;
 
            // Add several custom fields to the form
            let textfield = form.addField({
                id: 'custpage_textfield',
                type: 'text',
                label: 'Text'
            });
            let resultfield = form.addField({
                id: 'custpage_resultfield',
                type:'text',
                label: 'Result'
            });
            let sublistResultfield = form.addField({
                id: 'custpage_sublist_resultfield',
                type: 'text',
                label: 'Sublist Result Field'
            });
 
            // Get a reference to the sitecategory sublist
            let sublistObj = form.getSublist({
                id: 'sitecategory'
            });
             
            // Add a custom field to the sublist
            let subtextfield = sublistObj.addField({
                id: 'custpage_subtextfield',
                type: 'text',
                label: 'Sublist Text Field'
            });
 
            // Set the module path to the previous sample script
            form.clientScriptModulePath = './clientDemo.js';
             
            // Add two custom buttons to the form
            form.addButton({
                id: 'custpage_custombutton',
                label: 'SET_GET_VALUE',
                functionName: 'test_set_getValue'
            });
            form.addButton({
                id: 'custpage_custombutton2',
                label: 'SET_GETCURRENTSUBLISTVALUE',
                functionName: 'test_set_getCurrentSublistValue'
            });
        }
    };
}); 

        

General Notices