This article outlines the implementation of a user event script that automatically applies shipping costs based on the shipping country and order subtotal during the creation or update of Sales Orders in NetSuite.
To ensure consistency and compliance with regional shipping policies, the script dynamically sets the custbody_jj_custom_shippingcost field with the appropriate freight charge based on the following conditions:
✅ Canada
- If the shipping country is Canada (
CA) and the order subtotal is less than $49, a shipping charge of $12.50 is automatically applied.
✅ United States
- If the shipping country is United States (
US) and the order subtotal is less than $99, a shipping charge of $15.50 is automatically applied.
✅ General Behavior
- If the shipping cost field (
custbody_jj_custom_shippingcost) is already populated or the order total exceeds the threshold, the script does not override the value. - This logic runs in the Before Submit phase of the User Event Script on the Sales Order record.
The script addresses the issue where shipping costs were not automatically applied for certain regional conditions. It now enforces the following logic:
✅ Canada
- If
Shipping Country = Canada (CA)and Order Subtotal < $49- → then
custbody_jj_custom_shippingcost = $12.50
✅ United States
- If
Shipping Country = United States (US)and Order Subtotal < $99- → then
custbody_jj_custom_shippingcost = $15.50
✅ Additional Conditions
- If a valid value already exists in
custbody_jj_custom_shippingcost, no change is made. - The script operates during the Before Submit phase on Sales Orders.
Technical Implementation
🎯 Script Type:
User Event Script (@NScriptType UserEventScript)
📍 Execution Timing:
Before Submit
📦 Applies To:
Sales Order (record.Type.SALES_ORDER)
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([‘N/record’, ‘N/log’], function (record, log) {
function beforeSubmit(context) {
log.debug(“:hai test”);
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var newRecord = context.newRecord;
var shippingAddress = newRecord.getValue(‘shipaddress’);
var country = newRecord.getValue(‘shipcountry’); // ISO country code (e.g., CA, US)
var orderTotal = parseFloat(newRecord.getValue(‘subtotal’) || 0);
var customShippingCost = parseFloat(newRecord.getValue(‘custbody_jj_custom_shippingcost’) || 0);
// Log for debugging
log.debug(‘Shipping Address Debug’, shippingAddress);
log.debug(‘Country’, country);
log.debug(‘Order Total’, orderTotal);
log.debug(‘Existing Shipping Cost’, customShippingCost);
// Apply shipping logic only if not already set
if (!customShippingCost || customShippingCost === 0) {
if (country === ‘CA’ && orderTotal < 49) {
log.audit(‘Applying Shipping Cost’, ‘Canada < $49: $12.50 applied’);
newRecord.setValue({
fieldId: ‘custbody_jj_custom_shippingcost’,
value: 12.50
});
} else if (country === ‘US’ && orderTotal < 99) {
log.audit(‘Applying Shipping Cost’, ‘US < $99: $15.50 applied’);
newRecord.setValue({
fieldId: ‘custbody_jj_custom_shippingcost’,
value: 15.50
});
}
// Optional: Log updated value
var updatedShippingCost = newRecord.getValue(‘custbody_jj_custom_shippingcost’);
log.debug(‘Updated Shipping Cost’, updatedShippingCost);
}
}
}
return {
beforeSubmit: beforeSubmit
};
});