Scenario
You are the NetSuite administrator for your organization. You use the vendor record type to represent a vendor your organization works with, and you use the vendor bill record type to track bills for your vendors. You’ve added two custom fields to the vendor record type to store special department and class information for that vendor. When you edit a vendor bill record and change values in the item or expense sublists, you want to populate the Department and Class columns in the sublist with the custom field values from the associated vendor record.
Customization Details
The customization for this use case includes:
- Two custom fields (Department and Class) to hold department and class information for the vendor
- A client script triggered on the fieldChanged entry point
Steps in this tutorial to complete this customization:
- Before You Begin
- Step 1: Create the Custom Fields
- Step 2: Write the Script
- Step 3: Create the Script Record
- Step 4: Deploy the Script
- Step 5: Test the Solution
Step 1: Create the Custom Fields
This customization uses two custom fields. The Custom Department field stores department information for a vendor. The Custom Class field stores class information for a vendor. Both fields are custom entity fields added to the vendor record.
To create the Custom Department field:
- Go to Customization > Lists, Records, & Fields > Entity Fields > New.
Step 2: Write the Script
When a field is changed on the Expenses or Items sublist of a vendor bill, the script populates the Department and Class columns on the sublist. If a field on a different sublist is changed, or if a field that’s not on a sublist is changed, no sublist column values are populated.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define([‘N/search’, ‘N/log’], (search, log) => {
function fieldChanged(scriptContext) {
const sublistVal = scriptContext.sublistId;
if (sublistVal === ‘item’ || sublistVal === ‘expense’) {
const rec = scriptContext.currentRecord;
const vendorId = rec.getValue({
fieldId: ‘entity’
});
if (vendorId === ”) {
return;
}
const vendorLookup = search.lookupFields({
type: search.Type.VENDOR,
id: vendorId,
columns: [‘custentity_custom_department’,’custentity_custom_class’]
});
let classVal = ”;
let deptVal = ”;
classVal = vendorLookup[‘custentity_custom_class’][0].value;
deptVal = vendorLookup[‘custentity_custom_department’][0].value;
try {
rec.setCurrentSublistValue({
sublistId: sublistVal,
fieldId: ‘department’,
value: deptVal,
ignoreFieldChange: true
});
rec.setCurrentSublistValue({
sublistId: sublistVal,
fieldId: ‘class’,
value: classVal,
ignoreFieldChange: true
});
} catch(e) {
log.debug({
title: ‘Unable to set record values’,
details: ‘Unable to set record values for department and class’
});
}
}
}
return {
fieldChanged: fieldChanged
};
});