Hide a Column in a Sublist
This use case sample shows you how to hide a field column in a standard sublist.
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 invoice record type for your invoices. This record type includes everything you need to manage your invoices, but it also includes some fields you don’t need. In the Item sublist on an invoice record, you don’t need to populate the Item field. This field appears as a column in the Item sublist, and you want to use a script to hide this column before an invoice is edited.
Customization Details
The customization for this use case includes:
-
A user event script triggered on the beforeLoad 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: Write the Script
This script hides the Item column on the Item sublist for an invoice.
Script Summary
The following table summarizes the script for this tutorial:
Script: Hide Sublist Column |
|
---|---|
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, ue_hideSublistColumn.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 UserEventScript
* @NModuleScope SameAccount
*/
define(['N/ui/serverWidget', 'N/log'], (serverWidget, log) => {
function beforeLoad(context) {
if (context.type === context.UserEventType.EDIT) {
hideColumnField(context.form, 'item', 'item');
}
}
function hideColumnField(formObj, sublistId, fieldId) {
try {
const formSublist = formObj.getSublist({
id: sublistId
});
if (formSublist) {
const formField = formSublist.getField({
id: fieldId
});
if (formField && typeof formField !== 'undefined' && formField !== null) {
formField.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
});
}
}
} catch(error) {
log.error({
title: 'Error occurred when hiding field',
details: JSON.stringify({
sublistId: sublistId,
fieldId: fieldId
})
});
}
}
return {
beforeLoad: beforeLoad
};
});
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 user event script. The script uses two SuiteScript modules specified in the define
statement:
-
N/ui/serverWidget
– provides access to the NetSuite user interface including loaded forms -
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 UserEventScript
* @NModuleScope SameAccount
*/
define(['N/ui/serverWidget', 'N/log'], (serverWidget, log) => {
});
Create the entry point function
This script is triggered on the beforeLoad
entry point when an invoice record is loaded. The entry point function in this script determines if the record is being edited, and if so, it calls a helper function (that is also defined within the script).
Add the following function definition at the top of the define
function:
function beforeLoad(context) {
if (context.type === context.UserEventType.EDIT) {
hideColumnField(context.form, 'item', 'item');
}
}
Define the hideColumnField() helper function
This script uses a helper function to hide the Item column.
Add the following code after the beforeLoad function definition:
function hideColumnField(formObj, sublistId, fieldId) {
}
Add a try-catch block
The helper function in this script uses a try-catch block to log any errors that might occur during script execution.
Add the following code at the top of the hideColumnField()
helper function definition:
try {
} catch(error) {
log.error({
title: 'Error occurred when hiding field',
details: JSON.stringify({
sublistId: sublistId,
fieldId: fieldId
})
});
}
Get the Item sublist
To hide a field on a form, you need to get the field. Because the field to be removed is located on the Item sublist, you first need to get the sublist.
Add the following code inside the try section of the try-catch block:
const formSublist = formObj.getSublist({
id: sublistId
});
Hide the Item field
If the Item sublist exists on the invoice record being loaded, you want to get the field from the sublist and make sure that the field is defined. If the field is defined, you want to hide it.
Add the following code inside the try section of the try-catch block:
if (formSublist) {
const formField = formSublist.getField({
id: fieldId
});
if (formField && typeof formField !== 'undefined' && formField !== null) {
formField.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
});
}
}
Create the return statement
This script associates the beforeLoad
function with the beforeLoad
user event entry point.
Add the following code immediately above the closing });
in your script:
return {
beforeLoad: beforeLoad
};
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, ue_hideSublistColumn.js).
Step 2: 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
Hide Sublist Column
ID
_ue_hide_sublist_column
NetSuite prepends ‘customscript’ to this ID.
Description
This script hides the Item field on the Item sublist on an invoice record.
-
Optionally set any other fields on the script record as desired.
-
Click Save and Deploy. The Script Deployment page appears. Continue with Step 3: Deploy the Script.
Step 3: 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 2: Create the Script Record.
-
On the Script Deployment page, enter the following values:
Field
Value
Applies To
Invoice
ID
_ue_hide_sublist_column
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 a script to the Execution Log tab of the script deployment record as well as all errors.Execute As Role
Current Role
It is normally best practice to have scripts execute with the user’s current role to avoid granting unwanted access.
Audience > Roles
Check Select All
-
Click Save.
Step 4: Test the Solution
After you create the script record and deploy your script, you can test your solution by viewing and editing an existing invoice.
To test your solution:
-
Go to Transactions > Sales > Create Invoices > List.
-
Click View next to any existing invoice.
-
On the Invoice page, scroll down to the Item sublist and verify that the Item column appears.
-
Click Edit.
-
Scroll down to the Item sublist and verify that the Item column does not appear. Scroll to the right to see all columns on the sublist.
-
Click Cancel to close the invoice without editing it.
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
- 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
- Set Default Values in a Sublist
- Track Customer Deposit Balances
- Validate Order on Entry