Set Default Values in a Sublist
This use case shows you how to set default values for fields in a sublist. These values are taken from custom field values on a different record type.
This project is available on the SuiteCloud Project Repository on Oracle Samples GitHub and include complete project customization setup with custom objects, unit tests, package file configuration, and more.
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
The following table lists features, permissions, and other requirements necessary for performing this tutorial and implementing the solution:
Required Features |
The following features must be enabled in your account:
For more information, see Enabling Features. |
Required Permissions |
You will need a role with access to the following:
For more information, see NetSuite Permissions Overview |
Other Requirements |
None |
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.
-
Enter values for each field as listed in the following table:
Field
Value
Label
Custom Department
ID
_custom_department
NetSuite prepends ‘custentity’ to this ID.
Description
Stores department information for a vendor.
Type
List/Record
List/Record
Department
Applies To
Vendor
Display
Subtab: Main
-
Click Save.
To create the Custom Class field:
-
Go to Customization > Lists, Records, & Fields > Entity Fields > New.
-
Enter values for each field as listed in the following table:
Field
Value
Label
Custom Class
ID
_custom_class
NetSuite prepends ‘custentity’ to this ID.
Description
Stores class information for a vendor.
Type
List/Record
List/Record
Class
Applies To
Vendor
Display
Subtab: Main
-
Click Save.
For more information about creating custom entity fields, see the following help topics:
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.
Script Summary
The following table summarizes the script:
Script: Default Sublist Values |
|
---|---|
Script Type |
|
Modules Used |
|
Entry Points |
For more information about script types and entry points, see SuiteScript 2.x Script Types.
The Complete Script
This tutorial includes the complete script along with individual steps you can follow to build the script in logical sections. The complete script is provided below so that you can copy and paste it into your text editor and save the script as a .js file (for example, cs_defaultSublistValues.js).
If you would rather create your script by adding code in logical sections, follow the steps in Build the Script.
This tutorial script 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 Debugger.
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
/**
* @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
};
});
Build the Script
You can write the script using a step-by-step approach that includes the following:
The code snippets included below do not account for indentation. Refer to The Complete Script for suggested indentation.
Start with required opening lines
JSDoc comments and a define
function are required at the top of the script file. The JSDoc comments in this script indicate that it is a SuiteScript 2.1 client script. The script uses two SuiteScript modules specified in the define
statement:
-
N/search
– provides access to search functionality -
N/log
– allows you to log execution details
Start a new script file using any text editor and place the following JSDoc comments and define
function at the top of the file:
This tutorial script 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 Debugger.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/search', 'N/log'], (search, log) => {
});
Create the entry point function
This script is triggered on the fieldChanged
entry point when a column in the Expenses and Items sublist is changed.
Add the following function definition at the top of the define
function:
function fieldChanged(scriptContext) {
}
Check the sublist type
You are only interested in the item and expense sublists so verify that the current sublist is an item or expense sublist.
Add the following code at the top of the fieldChanged
function:
const sublistVal = scriptContext.sublistId;
if (sublistVal === 'item' || sublistVal === 'expense') {
}
Get the vendor class and department values
For item or expense sublists, you need to look up the vendor and retrieve the custom Department and Class fields for the vendor.
Add the following code inside the if
statement in the fieldChanged
function:
let rec = scriptContext.currentRecord;
let 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']
});
const classVal = '';
const deptVal = '';
classVal = vendorLookup['custentity_custom_class'][0].value;
deptVal = vendorLookup['custentity_custom_department'][0].value;
Set the record department and class fields
After you retrieve the custom Department and Class values for the vendor, you want to add those values to the vendor bill record. You will do this in a try-catch block so you can log any errors that might occur while updating the vendor record.
Add the following code inside the if
statement in fieldChanged
function:
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'
});
}
Create the return statement
This script associates the fieldChanged
function with the fieldChanged
client script entry point.
Add the following code immediately above the closing });
in your script:
return {
fieldChanged: fieldChanged
};
Save your script file
You need to save your script file so you can load it to the NetSuite File Cabinet. Before you save your script file, you may want to adjust the indentation so that the script is readable. Refer to The Complete Script for suggested indentation.
When you are happy with how your script file reads, save it as a .js file (for example, cs_defaultSublistValues.js).
Step 3: Create the Script Record
Now that you’ve completed the script, you can upload the script file to the File Cabinet and create a script record for it.
For more information about creating script records, see Creating a Script Record.
To create the script record:
-
Upload your script to the NetSuite File Cabinet.
-
Go to Customization > Scripting > Scripts > New.
-
Select your script from the Script File list and click Create Script Record. The Script page is displayed.
-
On the Script page, enter the following values:
Field
Value
Name
Default Sublist Values
ID
_cs_default_sublist_values
NetSuite prepends ‘customscript’ to this ID.
Description
This script sets the default values for the Department and Class columns in the sublist with the custom field values from the associated vendor record.
-
Optionally set any other fields on the script record as desired.
-
Click Save and Deploy. The Script Deployment page appears. Continue with Step 4: Deploy the Script.
Step 4: Deploy the Script
After you create the script record for your script, you can create a script deployment record for it. A script deployment record determines how, when, and for whom the script runs.
For more information about script deployment records, see Script Deployment.
To deploy the script:
-
Complete the steps in Step 3: Create the Script Record.
-
On the Script Deployment page, enter the following values:
Field
Value
Applies To
Vendor Bill
ID
_cs_default_sublist_values
NetSuite prepends 'customdeploy' to this ID.
Status
Testing
The Testing status allows the script owner to test the script without affecting other users in the account.
Log Level
Debug
The Debug level will write all
log.debug
statements in the script to the Execution Log tab of the script deployment record as well as all errors.Audience > Roles
Check Select All
-
Click Save.
Step 5: Test the Solution
After you create the script record and deploy your script, you can test your solution by creating a new vendor record and verifying the values in the Department and Class sublist columns.
To test your solution:
-
Go to Lists > Relationships > Vendors > New to create a new vendor.
-
On the Vendor page, enter the following values:
Field
Value
Company Name
Tutorial Vendor
Category
Select any value, for example, Subcontractor
Custom Department
Select any value, for example, Service
Custom Class
Select any value, for example, Recurring Business
-
Click Save.
-
Go to Transactions > Payables > Enter Bills to enter a new bill from the new Tutorial Vendor vendor.
-
On the Bill page, enter the following values:
Field
Value
Custom Form
Standard Vendor Bill
Vendor
Tutorial Vendor
All remaining required fields
Accept the default values
Expenses and Items tab: Account
Select any account
Expenses and Items tab: Amount
Enter any amount
-
Verify that as soon as you enter the Amount value, the Department and Class fields are automatically populated with the values you set when you created the Tutorial Vendor record.
-
You can leave the Bill page without finishing the bill because you only needed to test that the Department and Class fields on the vendor bill were automatically populated.
Related Topics
- SuiteCloud Customization Tutorials
- Add Custom Button to Execute a Suitelet
- Calculate Commission on Sales Orders
- Copy a Value to the Item Column
- Disable Tax Fields
- Hide a Column in a Sublist
- Populate Item Substitution
- Set a Default Posting Period in a Custom Field
- Set Purchase Order Exchange Rate
- Set the Item Amount to Zero for Marketing Orders
- Track Customer Deposit Balances
- Validate Order on Entry