N/transaction Module Script Sample
The following script sample demonstrates how to use the features of the N/transaction module.
For transaction types that use direct void, you must disable the Void Transactions Using Reversing Journals feature in your account to avoid an error being thrown. To disable this feature, go to Setup > Accounting > Preferences > Accounting Preferences.
Void a Sales Order Transaction
The following sample creates a sales order record, saves it, then voids the sales order. Before creating the sales order record, the sample loads a set of accounting preferences from the current NetSuite account, specifies that the REVERSALVOIDING
preference should be disabled (set to false
), and saves the preferences. This sample works only in NetSuite OneWorld accounts. Be sure to replace hard-coded values (such as record IDs) with valid values from your NetSuite account.
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/transaction', 'N/config', 'N/record'], function(transaction, config, record) {
function voidSalesOrder() {
var accountingConfig = config.load({
type: config.Type.ACCOUNTING_PREFERENCES
});
accountingConfig.setValue({
fieldId: 'REVERSALVOIDING',
value: false
});
accountingConfig.save();
var salesOrderObj = record.create({
type: 'salesorder',
isDynamic: false
});
salesOrderObj.setValue({
fieldId: 'entity',
value: 107
});
salesOrderObj.setSublistValue({
sublistId: 'item',
fieldId: 'item',
value: 233,
line: 0
});
salesOrderObj.setSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: 1,
line: 0
});
var salesOrderId = salesOrderObj.save();
var voidSalesOrderId = transaction.void({
type: record.Type.SALES_ORDER,
id: salesOrderId
});
var salesOrder = record.load({
type: 'salesorder',
id: voidSalesOrderId
});
// The value of the memo field is 'VOID'
var memo = salesOrder.getValue({
fieldId: 'memo'
});
}
voidSalesOrder();
});