As a NetSuite Admin, you often need to enforce data accuracy, perform real-time calculations, and monitor transactions that require special attention. This admin tip demonstrates how a User Event Script can help you:
- Enforce required custom fields before saving a record
- Automatically calculate totals from line items
- Log high-value Sales Orders for audit and review
This approach improves data quality, reduces manual checks, and adds transparency to high-risk transactions.
Sample User Event Script
/**
* @NApiVersion 2 .x
* @NScriptType UserEventScript
*
* User Event Script Example:
* - Validates a required custom field on Sales Order
* - Calculates total amount from item lines and writes to body field
* - Logs an audit entry for high-value orders
*/
define(['N/record', 'N/log'],
function(record, log) {
/**
* beforeSubmit event:
* Used for validating fields and calculating totals before the record is saved.
*/
function beforeSubmit(context) {
var newRecord = context.newRecord;
// 1. Validate that "Customer Type" custom field is present
var customerType = newRecord.getValue({ fieldId: 'custbody_customer_type' });
if (!customerType) {
throw 'Customer Type (custbody_customer_type) is required. Please fill in before saving.';
}
// 2. Sum 'amount' from each item line and store in a header-level custom field
var totalCustomAmount = 0;
var numLines = newRecord.getLineCount({ sublistId: 'item' });
for (var i = 0; i < numLines; i++) {
var amount = newRecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
totalCustomAmount += parseFloat(amount) || 0;
}
// Save the calculated total amount back to the header field
newRecord.setValue({
fieldId: 'custbody_total_line_amount',
value: totalCustomAmount
});
}
/**
* afterSubmit event:
* Used for operations that occur after the record is committed to the database, such as logging high-value orders.
*/
function afterSubmit(context) {
// Only log for newly created orders
if (context.type === context.UserEventType.CREATE) {
var orderId = context.newRecord.id;
// Load the fresh copy of the Sales Order to access the header field
var rec = record.load({
type: record.Type.SALES_ORDER,
id: orderId
});
// Get total from body field and order transaction ID
var totalCustomAmount = rec.getValue('custbody_total_line_amount');
var transactionId = rec.getValue('tranid') || '[Draft SO]';
var approvalThreshold = 20000; // Set threshold for high-value orders
// 3. Log an audit entry if the total amount exceeds the threshold
if (totalCustomAmount > approvalThreshold) {
log.audit({
title: 'High Value Sales Order Submitted',
details: 'Sales Order (' + transactionId + ', internalId: ' + orderId
+ ') exceeds $' + approvalThreshold
+ '. Amount: $' + totalCustomAmount
});
}
}
}
// Return script entry points
return {
beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});
- Field Validation: Prevents saving a Sales Order without the 'Customer Type' custom field.
- Calculation: Sums the line item 'amount' values and stores them in a header-level custom field.
- Logging: When a new Sales Order’s total exceeds the defined threshold (e.g., $20,000), logs an audit entry for tracking and review.
- Note: Replace custbody_customer_type and custbody_total_line_amount with your actual custom field IDs if different.
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to automate your transactions using SuiteScript? Feel free to share them here!