Overview: NetSuite provides the ability to customize business processes using SuiteScript, and one common use case is updating the shipping method based on certain criteria in a Sales Order. This article will discuss a User Event Script designed to automatically update the shipping method for a Sales Order based on whether the shipping address is residential or commercial.
Script Explanation:
This script defines a User Event Script that is triggered after a Sales Order (SO) is created, edited, or updated in NetSuite. It updates the shipping method based on custom address validation fields, ensuring that the appropriate shipping method is applied to the order.
Core Components of the Script:
- Shipping Method Constants: The script starts by defining an object
SHIP_METHODthat maps different shipping methods to their respective internal IDs. These constants represent the shipping methods available in NetSuite:
javascript
Copy
Edit
const SHIP_METHOD = {
"FLAT_RATE": 983664,
"FEDEX_HOME_DELIVERY": 876651,
"FEDEX_GROUND": 3216
};
- FLAT_RATE: The default shipping method.
- FEDEX_HOME_DELIVERY: For residential deliveries.
- FEDEX_GROUND: For commercial deliveries.
- updateShipMethod Function: This function updates the shipping method of a given Sales Order based on the internal ID of the selected shipping method.
javascript
Copy
Edit
function updateShipMethod(soId, shipMethod) {
try {
record.submitFields({
type: record.Type.SALES_ORDER,
id: soId,
values: { 'shipmethod': shipMethod },
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
} catch (error) {
log.error('error @updateShipMethod', error);
}
}
record.submitFieldsis used to submit the updated shipping method field (shipmethod) to the Sales Order.- The
optionsobject disables sourcing and ignores mandatory fields to ensure the update is successful without requiring additional fields to be populated.
- User Event Trigger – afterSubmit: The
afterSubmitfunction is the main logic handler, triggered after the Sales Order is created, edited, or updated. This function checks the type of event (CREATE,EDIT, orXEDIT) and evaluates conditions to update the shipping method based on the address type.
javascript
Copy
Edit
const afterSubmit = (scriptContext) => {
try {
if (scriptContext.type === scriptContext.UserEventType.CREATE ||
scriptContext.type === scriptContext.UserEventType.EDIT ||
scriptContext.type === scriptContext.UserEventType.XEDIT) {
let soRec = scriptContext.newRecord;
let soId = soRec.id;
let shipMethod, residential, commercial;
// Look up existing values if editing an existing Sales Order
if (scriptContext.type === scriptContext.UserEventType.XEDIT) {
let lookupValues = search.lookupFields({
type: record.Type.SALES_ORDER,
id: soId,
columns: ['shipmethod', 'custbodycustrecord_sales_addrssvalidat', 'custbodycustrecord_sales_addvalcomm']
});
shipMethod = lookupValues.shipmethod[0]?.value;
residential = lookupValues.custbodycustrecord_sales_addrssvalidat;
commercial = lookupValues.custbodycustrecord_sales_addvalcomm;
} else {
// Fetch values if creating a new Sales Order
shipMethod = soRec.getValue({ fieldId: 'shipmethod' });
residential = soRec.getValue({ fieldId: 'custbodycustrecord_sales_addrssvalidat' });
commercial = soRec.getValue({ fieldId: 'custbodycustrecord_sales_addvalcomm' });
}
// Apply shipping method logic based on address type
if (shipMethod == SHIP_METHOD.FLAT_RATE) {
if (residential && commercial) {
return;
} else if (residential === true) {
updateShipMethod(soId, SHIP_METHOD.FEDEX_HOME_DELIVERY);
} else if (commercial === true) {
updateShipMethod(soId, SHIP_METHOD.FEDEX_GROUND);
} else {
return;
}
}
}
} catch (error) {
log.error('Error @afterSubmit', error);
}
};
- Address Validation Fields: The script looks up two custom fields:
custbodycustrecord_sales_addrssvalidat(residential address validation).custbodycustrecord_sales_addvalcomm(commercial address validation).- Depending on whether the address is residential or commercial, the script updates the shipping method to FedEx Home Delivery or FedEx Ground.
How This Script Works:
- When a Sales Order is created or edited, the
afterSubmitfunction is triggered. - The script checks if the shipping method is FLAT_RATE and evaluates the residential or commercial address flags.
- If the address is residential, the shipping method is updated to FedEx Home Delivery.
- If the address is commercial, the shipping method is updated to FedEx Ground.
- If neither condition is met, no update occurs.
Benefits of This Automation:
- Consistency: Automates the process of assigning the correct shipping method based on the order’s address type, reducing manual intervention.
- Efficiency: Saves time by automatically updating shipping methods, ensuring orders are processed with the correct shipping method.
- Scalability: Easily adaptable to handle other shipping methods or custom logic for different types of addresses.
Conclusion:
This User Event Script streamlines the process of managing shipping methods based on address validation in NetSuite. By automatically updating the shipping method based on whether the address is residential or commercial, businesses can ensure faster, error-free order fulfillment. Customizations like this help automate repetitive tasks, reducing errors and improving efficiency in the order management process.