N/currentRecord Module Script Samples
The following script samples demonstrate how to use the features of the N/currentRecord module:
Update Fields on Current Record using a Custom Module Script and a User Event Script
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 Module by naming it in the define
statement. It must also retrieve a currentRecord.CurrentRecord object by using the currentRecord.get() method.
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.
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.
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.
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'
});
}
};
});
Perform Field Sourcing Synchronously
The following script sample shows how to use the forceSyncSourcing
parameter.
This parameter can be used to alleviate a timing situation that may occur in some browsers when fields are sourced. For some browsers, some APIs are triggered without waiting for the field sourcing to complete. For example, if forceSyncSourcing is set to false when adding sublist lines, the lines aren't committed as expected. Setting the parameter to true, forces synchronous sourcing.
This sample shows using the forceSyncSourcing parameter in the setCurrentSublistValue method. The forceSyncSourcing parameter is also available in the setText, setValue, setCurrentSublistText, setMatrixHeaderValue, and setCurrentMatrixSublistValue methods.
This sample script uses the require
function so that you can copy it into the SuiteScript Debugger and test it. You must use the define
function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
/**
* @NApiVersion 2.x
*/
require(['N/currentRecord'], function(currentRecord){
var rec = currentRecord.get();
rec.selectNewLine({
sublistId: 'item'
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: 39,
forceSyncSourcing:true
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 1,
forceSyncSourcing:true
});
rec.commitLine({sublistId: 'item'});
})