There are cases where a Sales Order remains open because some or all item lines are still marked as open, even if no further processing is needed. Manually closing each line can be time-consuming, especially when dealing with high-volume transactions.
If you need to programmatically close all item lines on a Sales Order—whether for cleanup, automation, or integration purposes—you can do so using a Scheduled Script. This script loads the Sales Order, iterates through each line on the item sublist, and sets the isclosed field to true to mark it as closed.
Here's an example:
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/search','N/record'],
function(search,record) {
function execute(scriptContext){
recordObj = record.load({
type:record.Type.SALES_ORDER,
id: ####, //SO ID
isDynamic:true
});
var lineCount = recordObj.getLineCount({
sublistId:'item'
});
for(var i=0;i<=(lineCount-1);i++){
var sublistLine = recordObj.selectLine({
sublistId:'item',line:i
});
recordObj.setCurrentSublistValue({
sublistId:'item',
fieldId:'isclosed',
value:true
});
recordObj.commitLine({
sublistId:'item'
});
}
recordObj.save({
enableSourcing:true,
ignoreMandatoryFields:true
});
}
return {
execute: execute
};
});
Code Breakdown
recordObj = record.load({
type: record.Type.SALES_ORDER,
id: ####, // SO internal ID
isDynamic: true
});
- Loads the Sales Order record using its internal ID.
- type: Specifies the record type (SALES_ORDER).
- id: The internal ID of the Sales Order you want to modify.
- isDynamic: true: Enables dynamic mode, allowing field manipulation line by line using selectLine() and commitLine().
var lineCount = recordObj.getLineCount({
sublistId: 'item'
});
- Retrieves the number of lines in the item sublist (i.e., how many line items are in the Sales Order).
- for (var i = 0; i <= (lineCount - 1); i++) {
- Loops through each item line in the sublist.
- Note: (lineCount - 1) ensures that the loop does not exceed the actual number of lines.
var sublistLine = recordObj.selectLine({
sublistId: 'item',
line: i
});
- Selects the current line in the item sublist to allow modifications in dynamic mode.
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'isclosed',
value: true
});
- Sets the isclosed field of the current line to true, effectively closing the line.
recordObj.commitLine({
sublistId: 'item'
});
- Saves the changes for the current line and moves to the next one.
- Required in dynamic mode to apply line changes.
recordObj.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
- Saves the entire Sales Order record after all item lines are marked closed.
- enableSourcing: true: Enables sourcing of default values for fields, if applicable.
- ignoreMandatoryFields: true: Allows saving even if required fields are not filled (use with caution).
For more information on handling scripts, refer to the following:
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.
How are you using Scheduled Script in your business? Share them in the NetSuite Admin Corner