ANS-1156 · SUITESCRIPT DEVELOPMENT

How to Programmatically Close a Sales Order in NetSuite via SuiteScript?

Learn the recommended SuiteScript method to effectively close sales orders by updating line item statuses, ensuring proper order management.

Short answer

To programmatically close a sales order in NetSuite, iterate through all its line items and set the 'isclosed' field to 'true' for each. This action updates the sales order status to 'Closed', which is the supported method for effectively preventing further processing of an order via SuiteScript.

Scenario

Users often seek to programmatically change a sales order's status to 'Canceled' using SuiteScript. While direct cancellation to a 'Canceled' status for approved orders is not officially supported via script, the business requirement is to prevent further processing of an order.

Solution

The recommended and supported method for effectively 'canceling' a sales order via SuiteScript, particularly for orders that are no longer in a 'Pending Approval' state, is to 'close' the order. This is achieved by setting all individual line items on the sales order to a 'Closed' status. When all lines are closed, the sales order's overall status will reflect 'Closed'.This process typically involves:

  1. Loading the sales order record.

  2. Iterating through each sublist line item (e.g., 'item' sublist).

  3. Setting the 'isclosed' field to 'true' for every line.

  4. Saving the updated sales order record.Example SuiteScript (SuiteScript 2.x recommended for current NetSuite versions) for closing all lines on a sales order:

javascript/** * @NApiVersion 2.x * @NModuleScope SameAccount */define(['N/record', 'N/log'], function(record, log) {    function closeSalesOrderLines(salesOrderId) {        try {            var salesOrder = record.load({                type: record.Type.SALES_ORDER,                id: salesOrderId,                isDynamic: false            });            var lineCount = salesOrder.getLineCount({                sublistId: 'item'            });            for (var i = 0; i < lineCount; i++) {                salesOrder.setSublistValue({                    sublistId: 'item',                    fieldId: 'isclosed',                    line: i,                    value: true                });            }            var updatedRecordId = salesOrder.save();            log.debug('Sales Order Closed', 'Sales Order ' + updatedRecordId + ' has been closed by setting all lines to isclosed: true.');            return updatedRecordId;        } catch (e) {            log.error('Error Closing Sales Order', 'Could not close sales order ' + salesOrderId + ': ' + e.message);            return null;        }    }    return {        closeSalesOrderLines: closeSalesOrderLines    };});

It is important to note that directly calling undocumented functions like 'cancel_approve_order()' or manipulating URLs such as 'salesordermanager.nl' with 'optype=cancel' are unsupported methods. These approaches are not recommended as they may lead to unpredictable behavior or cease to function with future NetSuite updates. The 'Canceled' status is generally reserved for sales orders that are still in a 'Pending Approval' state, where a formal cancellation process might be available.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant