Send Email After Sales Order Is Closed via Suitescript

Scripts and workflow do not execute when a transaction is closed. Once the status of the Sales Order is set to Closed, you cannot access the said record any longer because it would be locked by standard NetSuite settings.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record','N/email'], function (record,email){
    function beforeLoad(scriptContext) {
    	var recId = scriptContext.request.parameters.id;
    	var soRecord = record.load({
    		type: record.Type.SALES_ORDER,
    		id: recId,
    	})
    	var soStatus = soRecord.getValue({fieldId: 'status'})
    	var soSalesRep = soRecord.getValue({fieldId: 'salesrep'}) || 2

        if(soStatus=='Closed'){
           email.send({
    		author:2,
    		recipients:soSalesRep,
    		subject:"Sales Order "+recId+" is closed",
    		body: "Your order has now been closed.",
    	})
        }

    }

    return {
        beforeLoad: beforeLoad
    };
});

Leave a comment

Your email address will not be published. Required fields are marked *