Set the Item Amount to Zero for Marketing Orders
This use case shows you how to use a checkbox to indicate a sales order is a marketing order, so the amount is automatically set to zero.
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.
Customization Details
The customization for this use case includes:
-
A custom field (Marketing Order) that is used to indicate a marketing order
-
A user event script triggered on the afterSubmit 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 need a role with access to the following:
For more information, see NetSuite Permissions Overview. |
Other Requirements |
None |
Step 1: Create the Custom Field
This customization uses a custom field. The Marketing Order custom field indicates if the sales order is a marketing order. It's a transaction body field added to the sales record.
To create the custom field:
-
Go to Customization > Lists, Records, Fields > Transaction Body Fields > New.
-
Enter or select values for fields as listed in the following table:
Field
Value
Label
Marketing Order
ID
_marketing_order
NetSuite prefixes the ID with custbody.
Type
Check Box
Description
Indicates whether the sales order is a marketing order.
Applies To
Sale
Display
Subtab: Main
-
Click Save.
For more information about creating custom fields, see the following help topics:
Step 2: Write the Script
This script determines if the sales order being saved is a marketing order. If it's a marketing order, the script sets the Amount field on the sales order to zero.
Script Summary
The following table summarizes the script:
Script: Marketing 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 it, paste it into your text editor, and save it as a .js file (for example, ue_marketingOrder.js).
If you would rather create your script step by step, 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). 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.
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/log'], (record, log) => {
function afterSubmit(context) {
try {
const currentRecord = context.newRecord;
const recordID = currentRecord.id;
const recordType = currentRecord.type;
const objSalesOrder = record.load({
type: recordType,
id: recordID,
isDynamic: true
});
const isMktgSalesOrder = objSalesOrder.getValue({
fieldId: 'custbody_marketing_order'
});
if (isMktgSalesOrder) {
const intItemLines = objSalesOrder.getLineCount({
sublistId: 'item',
});
for (let i = 0; i < intItemLines; i++) {
objSalesOrder.selectLine({
sublistId: 'item',
line: i
});
objSalesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: 0
});
objSalesOrder.commitLine({
sublistId: 'item'
});
}
objSalesOrder.save();
}
} catch(e) {
log.error({
title: 'marketing order script - afterSubmit',
details: e.message
});
}
}
return {
afterSubmit: afterSubmit
};
});
Build the Script
You can write the script using a step-by-step approach that includes the following:
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 user event script. The script uses two SuiteScript modules specified in the define
statement:
-
N/record
– provides access to or use of NetSuite records -
N/log
– allows you to log execution details
Start a new script file using any text editor and add the following JSDoc comments and define
function at the top:
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 UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/log'], (record, log) => {
});
Create the entry point function
This script is triggered on the afterSubmit
entry point when a sales order is saved. The script uses a try-catch block to log any errors that might occur during script execution. Most of the script code is placed in the try block.
Add the following function definition and initial try-catch block statements at the top of the define
function:
function afterSubmit(context) {
try {
} catch(e) {
log.error({
title: 'marketing order script - afterSubmit',
details: e.message
});
}
}
Load the sales order that is being saved
You need to load the sales order record to determine if it's a marketing order.
Add the following code inside the try block:
const currentRecord = context.newRecord;
const recordID = currentRecord.id;
const recordType = currentRecord.type;
const objSalesOrder = record.load({
type: recordType,
id: recordID,
isDynamic: true
});
Determine if the sales order is a marketing order
You need to determine if the sales order is a marketing order by checking the value of the Marketing Order custom field.
Add the following code inside the try block:
const isMktgSalesOrder = objSalesOrder.getValue({
fieldId: 'custbody_marketing_order'
});
Set the amount for all items on the sales order to zero
If the sales order is a marketing order, you need to set the Amount for each item on the sales order to 0.
Add the following code inside the try block:
if (isMktgSalesOrder) {
const intItemLines = objSalesOrder.getLineCount({
sublistId: 'item',
});
for (let i = 0; i < intItemLines; i++) {
objSalesOrder.selectLine({
sublistId: 'item',
line: i
});
objSalesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: 0
});
objSalesOrder.commitLine({
sublistId: 'item'
});
}
objSalesOrder.save();
}
Create the return statement
This script associates the afterSubmit
function with the afterSubmit
user event entry point.
Add the following code immediately above the closing });
in your script:
return {
afterSubmit: afterSubmit
};
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, ue_marketingOrder.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:
-
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
Marketing Order
ID
_ue_marketing_order
NetSuite prefixes the ID with customscript.
Description
This script sets the amount value to 0 for all items on a marketing (sales) order.
-
Set any other fields on the script record as required.
-
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:
-
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
_ue_marketing_order
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.Execute As Role
Current Role
It is normally best practice to have scripts run with the user’s current role to avoid granting unwanted access.
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 it by creating a sales order with multiple items, marking it as a marketing order, and saving it. After it is saved, verify that all item values are 0.
To test your solution:
-
Go to Transaction > Sales > Enter Sales Orders.
-
Select the Standard Sales Order form and verify that the Marketing Order box appears on the form. This field appears on all standard and custom sales order forms, but for this tutorial, you can use the Standard Sales Order form.
-
Add multiple items to the sales order. You don't need to change any field for the item unless you want to. Ensure that the Amount field for each item isn't already 0.
-
Fill out all other required fields.
-
Check the Marketing Order box.
-
Click Save.
-
View the new sales order and verify that the Marketing Order box is checked and that the Amount for all items is 0.
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 Default Values in a Sublist
- Track Customer Deposit Balances
- Validate Order on Entry