When creating or editing Sales Orders, you may find yourself needing to apply the same Location across all item lines. For example, you might want all items to ship from the same warehouse, or you may need to ensure that fulfillment is handled by a single distribution center. Doing this manually—especially for orders with many line items—can be time-consuming and error-prone.
To simplify the process, you can leverage SuiteScript to automatically update all item lines with a single Location. This approach ensures accuracy, reduces manual effort, and helps keep your order management consistent.
Important: Ensure the Location field is exposed on your Sales Order form; otherwise, it won’t appear in the Item sublist and the script won’t be able to update it.
Below is a sample Scheduled Script that updates all item lines with the same Location:
/**
* @NApiVersion 2 .x
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/log'], function(search, record, log) {
function execute(context) {
try {
var salesOrderId = 12345; // <-- your Sales Order internal ID
var newLocationId = 1; // <-- your target location's internal ID
var soRecord = record.load({
type: record.Type.SALES_ORDER,
id: salesOrderId
});
var lineCount = soRecord.getLineCount({ sublistId: 'item' });
for (var i = 0; i < lineCount; i++) {
soRecord.setSublistValue({
sublistId: 'item',
fieldId: 'location',
line: i,
value: newLocationId
});
var itemId = soRecord.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
log.debug('Updated Item Line', 'Line: ' + i + ', Item ID: ' + itemId + ', New Location: ' + newLocationId);
}
// Save your changes!
var updatedId = soRecord.save();
log.audit('Sales Order Updated', 'Record ID: ' + updatedId);
} catch (e) {
log.error('Error Updating Sales Order', e);
}
}
return {
execute: execute
};
});
Code Snippet | Description |
|---|
var salesOrderId = 12345;var newLocationId = 1; | Define the internal IDs for the Sales Order and the target Location. Update these values as needed. |
var soRecord = record.load({ type: record.Type.SALES_ORDER, id: salesOrderId }); | Load the Sales Order record using its internal ID. |
var lineCount = soRecord.getLineCount({ sublistId: 'item' }); | Get the number of lines (items) in the Items sublist. |
for (var i = 0; i < lineCount; i++) { ... } | Iterate through each item line in the sublist. |
soRecord.setSublistValue({ sublistId: 'item', fieldId: 'location', line: i, value: newLocationId }); | Set the Location field for each item line to the new location. |
var itemId = soRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }); | Get the Item internal ID for logging or further use. |
log.debug('Updated Item Line', ...); | Log the update details (line number, Item ID, new Location) for verification. |
var updatedId = soRecord.save(); | Save the Sales Order record with all the line-level changes. |
log.audit('Sales Order Updated', 'Record ID: ' + updatedId); | Log the completion and final Sales Order ID for auditing. |
catch (e) { log.error('Error Updating Sales Order', e); } | Catch and log any errors encountered during processing for troubleshooting. |
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!