Call a Macro on a Sales Order Record
The following sample shows you how to call a calculateTax macro on a sales order record. To execute a macro on a record, the record must be created or loaded in dynamic mode. Note that the SuiteTax feature must be enabled to successfully execute the macro used in this sample.
For information about record macros, see Overview of Record Action and Macro APIs.
Note:
This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.1 Script Basics and SuiteScript 2.1 Script Types.
/**
* @NApiVersion 2.1
*/
require(['N/record'], (record) => {
const recordObj = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true
});
const entityValue = 1;
const itemValue = 1;
recordObj.setValue({
fieldId: 'entity',
value: entityValue
});
recordObj.selectNewLine({ sublistId: 'item' });
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: itemValue
});
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 1
});
recordObj.commitLine({ sublistId: 'item' });
const totalBeforeTax = recordObj.getValue({ fieldId: 'total' });
const macros = recordObj.getMacros();
if ('calculateTax' in macros) {
macros.calculateTax();
}
const totalAfterTax = recordObj.getValue({ fieldId: 'total' });
const recordId = recordObj.save({
enableSourcing: false,
ignoreMandatoryFields: false
});
log.debug({
title: 'Sales order created',
details: { recordId, totalBeforeTax, totalAfterTax }
});
});