Validate Order on Entry
This use case shows you how to validate an order to make sure that the number of items in the order corresponds to the number of cases of that item that can fit on a pallet (that is, only accept orders fulfilled by full cases). This use case also shows you how to verify that the total weight of the order does not exceed the maximum allowed weight for a single order.
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.
Customization Details
This customization for this use case includes:
-
A custom field (Cases per Pallet) that is used to store the number of cases of the item that are included on one pallet
-
A custom field (Item Weight) that is used to store the weight of a single item
-
A client script triggered on the validateLine and saveRecord entry points
-
A script parameter (Max Weight ) that is used to store the maximum weight for an order
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 Cases per Pallet field stores the number of cases of a particular item that fit on one pallet. The Item Weight field stores the weight of a single item. Both fields are custom transaction line fields added to the Item sublist on a sales order record.
To create the Cases per Pallet field:
-
Go to Customization > Lists, Records, Fields > Transaction Line Fields > New.
-
Enter or select values for fields as listed in the following table:
Field
Value
Label
Cases per Pallet
ID
_cases_per_pallet
NetSuite prepends ‘custcol’ to this ID.
Type
Integer Number
Store Value
Checked
Applies To
Sale Item
-
Click Save.
To create the Item Weight field:
-
Go to Customization > Lists, Records, Fields > Transaction Line Fields > New.
-
Enter or select values for fields as listed in the following table:
Field
Value
Label
Item Weight
ID
_item_weight
NetSuite prepends ‘custcol’ to this ID.
Type
Decimal Number
Store Value
Checked
Applies To
Sale Item
-
Click Save.
For more information about creating custom entity fields, see the following help topics:
Step 2: Write the Script
This script validates an order to make sure that the number of items in the order corresponds to the number of cases of that item that can fit on a pallet (that is, only accept orders fulfilled by full cases), and verifies that the total weight of the order does not exceed the maximum allowed weight for a single order.
Script Summary
The following table summarizes the script:
Script: Validate Order |
|
---|---|
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_validateOrder.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/runtime', 'N/log'], (, runtime, log) => {
function validateLine(scriptContext) {
const recSalesOrder = scriptContext.currentRecord;
if (scriptContext.sublistId === 'item') {
const casePerPallet = recSalesOrder.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cases_per_pallet'
});
const quantity = recSalesOrder.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
});
if (quantity % casePerPallet !== 0) {
alert(`Please order in multiples of ${casePerPallet} Case/Pallet for this item.`);
return false;
} else {
return true;
}
}
return true;
}
function saveRecord(scriptContext) {
const scriptObj = runtime.getCurrentScript();
const weightParam = scriptObj.getParameter({
name: 'custscript_max_weight'
});
if (NSUtil.isEmpty(weightParam)) {
log.error({
title: 'saveRecord',
details: 'Max weight script parameter not defined'
});
return;
}
const recSalesOrder = scriptContext.currentRecord;
const lineCount = recSalesOrder.getLineCount({
sublistId: 'item'
});
let totalWeight = 0;
for (let i = 0; i < lineCount; i++) {
const itemWeight = recSalesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_item_weight',
line: i
});
const quantity = recSalesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
const lineWeight = itemWeight * quantity;
totalWeight += lineWeight;
}
if (totalWeight > weightParam) {
alert(`The total weight of the order is ${totalWeight}. The max weight is ${weightParam}. Please adjust the order and submit a separate order if necessary.`);
return false;
}
return true;
}
const NSUtil = {};
NSUtil.isEmpty = function(stValue) {
return(
stValue === "" || stValue === null || stValue === undefined ||
(stValue.constructor === Array && stValue.length === 0) ||
(stValue.constructor === Object &&
(function(v){
for (const k in v) return false;
return true;
})(stValue))
);
}
return {
validateLine: validateLine,
saveRecord: saveRecord
};
});
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.x (2.0 or 2.1) client script. The script uses three SuiteScript modules specified in the define
statement:
-
N/runtime
– provides access to script parameters -
N/log
– allows you to 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:
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/runtime', 'N/log'], (runtime, log) => {
});
Create the entry point functions
This script is triggered on the validateLine
entry point when an item is added to a sales order and on the saveRecord
entry point when a sales order is saved.
Add the following function definitions at the top of the define
function:
function validateLine(scriptContext) {
return true;
}
function saveRecord(scriptContext) {
}
Get the current record
This script operates on the current sales order record. You will need to get the current sales order record.
Add the following code at the top of the validateLine
function above the return true;
statement:
const recSalesOrder = scriptContext.currentRecord;
Validate the cases per pallet
You need to validate the value entered in the Cases Per Pallet column for each item on the sales order record. The Quantity entered for each item must correspond to the number of cases allowed per pallet for that item.
Add the following code to the validateLine
function above the return true;
statement:
if (scriptContext.sublistId === 'item') {
const casePerPallet = recSalesOrder.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cases_per_pallet'
});
const quantity = recSalesOrder.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
});
if (quantity % casePerPallet !== 0) {
alert(`Please order in multiples of ${casePerPallet} Case/Pallet for this item.`);
return false;
} else {
return true;
}
}
Get the script parameter
When the sales order record is saved, you need to get the value of the script parameter (custscript_max_weight to be set in Step 5) that specifies the maximum weight of an order.
Add the following code at the top of the saveRecord
function:
const scriptObj = runtime.getCurrentScript();
const weightParam = scriptObj.getParameter({
name: 'custscript_max_weight'
});
if (NSUtil.isEmpty(weightParam)) {
log.error({
title: 'saveRecord',
details: 'Max weight script parameter not defined'
});
return;
}
Validate the total weight of the order
You also need to verify that the total weight of the order does not exceed the maximum weight allowed as specified by script parameter (custcol_item_weight to be set in Step 5).
Add the following code to the saveRecord
function:
const recSalesOrder = scriptContext.currentRecord;
const lineCount = recSalesOrder.getLineCount({
sublistId: 'item'
});
let totalWeight = 0;
for (let i = 0; i < lineCount; i++) {
const itemWeight = recSalesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_item_weight',
line: i
});
const quantity = recSalesOrder.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
const lineWeight = itemWeight * quantity;
totalWeight += lineWeight;
}
if (totalWeight > weightParam) {
alert(`The total weight of the order is ${totalWeight}. The max weight is ${weightParam}. Please adjust the order and submit a separate order if necessary.`);
return false;
}
return true;
Create the isEmpty function
This script uses a utility function to determine if the value of the script parameter is empty.
Add the following code after the end of the saveRecord
function:
const NSUtil = {};
NSUtil.isEmpty = function(stValue) {
return(
stValue === "" || stValue === null || stValue === undefined ||
(stValue.constructor === Array && stValue.length === 0) ||
(stValue.constructor === Object &&
(function(v){
for (const k in v) return false;
return true;
})(stValue))
);
}
Create the return statement
This script associates the validateLine
function with the validateLine
client script entry point, and the saveRecord
function with the saveRecord
client script entry point.
Add the following code immediately above the closing });
in your script:
return {
validateLine: validateLine,
saveRecord: saveRecord
};
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_validateOrder.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
Validate Order
ID
_cs_validate_order
NetSuite prepends ‘customscript’ to this ID.
Description
This script validates the number of items in an order to make sure that the number of items in the order corresponds to the number of cases of that item that can fit on a pallet for that item (that is, only accept orders fulfilled by full cases), and verifies that the total weight of the order does not exceed the maximum allowed weight for a single order.
-
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
Sales Order
ID
_cs_validate_order
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: Create and Set the Script Parameter
This tutorial uses a script parameter to specify the maximum allowed weight for a sales order.
For more information about creating and setting script parameters, see Creating Script Parameters (Custom Fields).
To create the script parameter:
-
Go to Customization > Scripting > Scripts.
-
Locate your script in the list of scripts and click Edit next to the script name.
-
On the Script page, click the Parameters tab and click New Parameter. The Script Field page appears.
-
On the Script Field page, enter the following values:
Script Parameter
Label
ID
Type
Description
Preference
Maximum Weight
Maximum Weight
_max_weight
NetSuite prepends ‘custscript’ to this ID. The value ‘custscript_
max_weight’ is used in the script. Decimal Number
The maximum allowed weight for a sales order.
Leave blank
-
Click Save to save the script parameter. The Script page appears.
-
Click Save to save the script record with the updated script parameter.
After you create the script parameter, you must set the value of the parameter on the script deployment record that you created in Step 4.
To set the script parameter:
-
Go to Customization > Scripts > Script Deployments.
-
Locate your script deployment in the list of script deployments and click Edit next to the script deployment ID.
-
On the Parameters tab, enter 25 for the Maximum Weight script parameter.
-
Click Save.
Step 6: Test the Solution
After you create the script record and deploy your script, you can test your solution by creating a sales order and adding items.
To test your solution:
-
Go to Transactions > Sales > Enter Sales Orders to create a new sales order.
-
Fill in all required fields on the sales order with any values you want. You can use the Standard Sales Order form.
-
On the Items subtab, add any item. Set the Quantity to 17, the Cases Per Pallet to 10, and the Item Weight to 5.
-
Click Add to add the item. Verify that because the Quantity of the item (17) is not a value that corresponds to the Cases Per Pallet (10), an error occurs: “Please order in multiples of 10 Cases/Pallet for this item” (which is text set in the script).
-
Close the error message and change the Quantity value to 10 and then click Add . Verify that the item was successfully added. You now have an order of 10 items each weighing 5pounds.
-
Click Save. Verify that the order is not saved because the weight of the order (10 x 5 lbs) is greater than the maximum weight allowed (25 as set in the script parameter). An error is displayed: “The total weight of the order is 50. The max weight is 25. Please adjust the order and submit a separate order if necessary” (which is text set in the script).
-
Edit the order so that the Item Weight is 1. This will make the total order weight 10 lbs.
-
Click Save. Verify that the order is successfully saved.
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
- Set Default Values in a Sublist
- Track Customer Deposit Balances