Set the Item Amount to Zero for Marketing Orders
This use case shows you how to use a box field to indicate that a sales order is a marketing order so the amount is automatically set to zero dollars.
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
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 will 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 will indicate that the sales order is a marketing order. It is 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 prepends ‘custbody’ to this ID.
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 is a marketing order, the script sets the Amount field on the sales order to zero (0).
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 and paste it into your text editor and save the script as a .js file (for example, ue_marketingOrder.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/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 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/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 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/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. A try-catch block is used to log any errors that might occur during script execution. Most of the script code will be 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 will need to load the sales order record to determine if it is 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 will need to determine if the sales order is a marketing order. This is done 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 value 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 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_marketingOrder.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
Marketing Order
ID
_ue_marketing_order
NetSuite prepends ‘customscript’ to this ID.
Description
This script sets the amount value to 0 for all items on a marketing (sales) 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
_ue_marketing_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 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 5: Test the Solution
After you create the script record and deploy your script, you can test your solution by creating a sales order with multiple items, marking it as a marketing order, and saving it. After it is saved, you can view it to 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 field is shown on the form. This field will be included on all standard and custom sales order forms. For this tutorial, you can use the Standard Sales Order form.
-
Add multiple items to the sales order. You do not need to adjust any field for the item unless you want to. Ensure that the Amount field for each item is not already 0.
-
Fill out all other required fields.
-
Check the Marketing Order box.
-
Click Save.
-
View the newly created 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