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.x Script Basics and SuiteScript 2.x Script Types.
/**
* @NApiVersion 2.x
*/
require(['N/record'],
function(record) {
var recordObj = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true
});
var ENTITY_VALUE = 1;
var ITEM_VALUE = 1;
recordObj.setValue({
fieldId: 'entity',
value: ENTITY_VALUE
});
recordObj.selectNewLine({
sublistId: 'item'
});
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: ITEM_VALUE
});
recordObj.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 1
});
recordObj.commitLine({
sublistId:'item'
});
var totalBeforeTax = recordObj.getValue({fieldId: 'total'});
// get macros available on the record
var macros = recordObj.getMacros();
// execute the macro
if ('calculateTax' in macros)
{
macros.calculateTax(); // For promise version use: macros.calculateTax.promise()
}
// Alternative (direct) macro execution
// var calculateTax = recordObj.getMacro({id: 'calculateTax'});
// calculateTax(); // For promise version use: calculateTax.promise()
var totalAfterTax = recordObj.getValue({fieldId: 'total'});
var recordId = recordObj.save({
enableSourcing: false,
ignoreMandatoryFields: false
});
});
Related Topics
- N/record Module Script Samples
- Create and Save a Contact Record
- Create and Save a Contact Record Asynchronously Using Promise Methods
- Create Multiple Sales Records Using a Scheduled Script
- Access Sublists and a Subrecord from a Record
- Access Sublists and a Subrecord from a Record Asynchronously Using Promise Methods
- SuiteScript 2.x Modules
- SuiteScript 2.x