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 fields on another record type.

This project is available on the SuiteCloud Project Repository on Oracle Samples GitHub. It includes a complete project customization setup with custom objects, unit tests, package file configuration, and more.

Scenario

You're the NetSuite administrator for your organization. You use the vendor record type for vendors your organization works with, and the vendor bill record type to track their bills. You’ve added two custom fields to the vendor record type to store special department and class information for each vendor. When you edit a vendor bill record and change values in the item or expense sublists, you want the Department and Class columns in the sublist to fill in with the custom field values from the 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:

  • Client SuiteScript – Lets you apply client scripts to records and forms.

  • File Cabinet – Lets you store your script files in the NetSuite File Cabinet.

For more information, see Enabling Features.

Required Permissions

You will need a role with access to the following:

  • Scripts – Edit access

  • Script Deployments – Edit access

  • Custom Entity Fields – Create access

For more information, see NetSuite Permissions Overview

Other Requirements

None

Step 1: Create the Custom Fields

This customization uses two custom entity fields that are added to the vendor record. The Custom Department field stores department information for the vendor, and the Custom Class field stores class information.

To create the Custom Department field:

  1. Go to Customization > Lists, Records, & Fields > Entity Fields > New.

  2. Enter values for each field as listed in the following table:

    Field

    Value

    Label

    Custom Department

    ID

    _custom_department

    NetSuite prefixes the ID with custentity.

    Description

    Stores department information for a vendor.

    Type

    List/Record

    List/Record

    Department

    Applies To

    Vendor

    Display

    Subtab: Main

  3. Click Save.

To create the Custom Class field:

  1. Go to Customization > Lists, Records, & Fields > Entity Fields > New.

  2. Enter values for each field as listed in the following table:

    Field

    Value

    Label

    Custom Class

    ID

    _custom_class

    NetSuite prefixes the ID with custentity.

    Description

    Stores class information for a vendor.

    Type

    List/Record

    List/Record

    Class

    Applies To

    Vendor

    Display

    Subtab: Main

  3. 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 fills in 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

SuiteScript 2.x Client Script Type

Modules Used

  • N/search Module

  • N/currentRecord Module – This module is available to all scripts as a provided context object. You don't need to explicitly load this module as a dependency in your define or require statement, however, you may if you want. This tutorial doesn't explicitly load this module.

  • N/log Module – This module is available to all scripts as a global object. However, you should explicitly load this module to avoid conflicts with other objects that may be named ‘log’.

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 it, paste it into your text editor, and save it as a .js file (for example, cs_defaultSublistValues.js).

If you would rather create your script step by step, follow the steps in Build the Script.

Note:

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). If you want to copy the script into the SuiteScript Debugger and test it, you must use the require function. For more information, see SuiteScript Debugger.

Important:

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:

Note:

The code snippets included below don't include indentation. Refer to The Complete Script for suggested formatting.

Start with required opening lines

You need JSDoc comments and a define function at the top of the script file. The JSDoc comments in this script indicate that it's a SuiteScript 2.1 client script. The script uses two SuiteScript modules in the define statement:

  • N/search – provides access to search functionality

  • N/log – lets you log execution details

Start a new script file in any text editor and add the following JSDoc comments and define function at the top of the file:

Note:

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). If you want to copy the script into the SuiteScript Debugger and test it, you must use the require function. 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're only interested in the item and expense sublists, so verify that the current sublist is one of those.

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.

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 them to the vendor bill record. You 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 upload it to the NetSuite File Cabinet. Before you save your script file, you may want to adjust the indentation to improve the readability of the script. Refer to The Complete Script for suggested formatting.

When you're happy with your script, 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 it 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:

  1. Upload your script to the NetSuite File Cabinet.

  2. Go to Customization > Scripting > Scripts > New.

  3. Select your script from the Script File list and click Create Script Record. The Script page is displayed.

  4. On the Script page, enter the following values:

    Field

    Value

    Name

    Default Sublist Values

    ID

    _cs_default_sublist_values

    NetSuite prefixes the ID with customscript.

    Description

    This script sets the default values for the Department and Class columns in the sublist using the custom field values from the vendor record.

  5. Set any other fields on the script record as required.

  6. 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, 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:

  1. Complete the steps in Step 3: Create the Script Record.

  2. On the Script Deployment page, enter the following values:

    Field

    Value

    Applies To

    Vendor Bill

    ID

    _cs_default_sublist_values

    NetSuite prefixes the ID with customdeploy.

    Status

    Testing

    The Testing status lets the script owner test the script without affecting other users in the account.

    Log Level

    Debug

    The Debug level writes all log.debug statements and errors to the Execution Log tab of the script deployment record.

    Audience > Roles

    Check Select All

  3. Click Save.

Step 5: Test the Solution

After you create the script record and deploy your script, you can test it by creating a new vendor record and verifying the values in the Department and Class sublist columns.

To test your solution:

  1. Go to Lists > Relationships > Vendors > New to create a new vendor.

  2. 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

  3. Click Save.

  4. Go to Transactions > Payables > Enter Bills to enter a new bill from the new Tutorial Vendor vendor.

  5. 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

  6. Verify that as soon as you enter the Amount, the Department and Class fields fill in automatically with the values you set for the Tutorial Vendor.

  7. You can leave the Bill page without finishing because you only needed to test that the Department and Class fields on the vendor bill filled in automatically.

Related Topics

General Notices