• : 1-877-596-8218
  • : sales@aecc.ca
# # #

How to close Work Orders on the Sales Order when SO is closed using SuiteScripts.

There is a native button on the sales order that can close the SO. As shown below:

If the Sales Order is closed, the user can fetch field values from the Sales Order but cannot make any changes on the Sales Order.
In some cases, the user want to also closed the WO on the line item sublist that created on this Sales Order.
We can use a User Event Before Load script to achieve this.Since the click on “Close Order”.
Button will not trigger Before Submit or After Submit User Event Script.

Here is the solution:
Every time the “Close Order” button clicked, the current Sales Order will be reloaded and become in “Closed” status. The Before Load User Event Script will check this status and the “Is WO Closed” field. Only when the status equal to “Closed” and “Is WO Closed” is false, the script will execute and find all Work Orders on the item sublist and close them. After This action finished, the “Is WO Closed” will set to true.

function beforeLoad_closeAllWO(type, form, request) {
	var status = nlapiGetFieldValue('status');
	if (type == 'view' && status == 'Closed' ) {
		var lineCount = nlapiGetLineItemCount('item');
		for (var i = 1; i <= lineCount; i++) {
			var WOID = nlapiGetLineItemValue('item', 'woid', i);
			if (!isEmpty(WOID)) {
				var WO = nlapiLoadRecord('workorder', WOID);
				var WOStatus = WO.getFieldValue('status');
				if (WOStatus != 'Closed') {
					WO.setFieldValue('orderstatus', 'B');
					var WOLineCount = WO.getLineItemCount('item');
					for (var j = 1; j <= WOLineCount; j++) {
						WO.setLineItemValue('item', 'isclosed', j, 'T');

After user clicks the close button, the related Work Order will also be closed, as shown below