ANS-0338 · SUITESCRIPT DEVELOPMENT
How to Retrieve a Transaction ID in NetSuite After Submit Scripts?
This guide outlines strategies for retrieving the transaction ID in NetSuite afterSubmit scripts, addressing common timing issues.
Short answer
To reliably obtain a transaction ID in an afterSubmit script, reload the record using `record.load` or look up the field using `search.lookupFields`. For scenarios involving large datasets or complex asynchronous processing, consider implementing a SuiteScript 2.x scheduled script or a Map/Reduce script.
Scenario
When an email is sent or a transaction is submitted in NetSuite, the internal transaction ID (tranid) may not be immediately available within the `afterSubmit` user event script context. This timing issue can prevent scripts from accurately referencing the newly created or updated transaction.
Solution
To ensure the transaction ID is available in an afterSubmit script, implement one of the following methods:
Reload the Record (SuiteScript 2.x):
The most direct approach is to reload the record within the
afterSubmitscript. This ensures that the script retrieves the most current state of the record, including the generated transaction ID.
javascript
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record'], function(record) {
function afterSubmit(scriptContext) {
var newRecord = scriptContext.newRecord;
var recordId = newRecord.id;
var recordType = newRecord.type;
// Reload the record to get the latest data, including the transaction ID
var loadedRecord = record.load({
type: recordType,
id: recordId
});
var transactionId = loadedRecord.getValue({ fieldId: 'tranid' });
log.debug('Transaction ID (reloaded)', transactionId);
// Further processing using transactionId
}
return { afterSubmit: afterSubmit };
});Look Up the Field (SuiteScript 2.x):
Alternatively, use
search.lookupFieldsto retrieve specific fields, such as the transaction ID, from the saved record. This is often more efficient than loading the entire record if only a few fields are needed.
javascript
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/search'], function(search) {
function afterSubmit(scriptContext) {
var newRecord = scriptContext.newRecord;
var recordId = newRecord.id;
var recordType = newRecord.type;
// Look up the 'tranid' field from the saved record
var lookupResult = search.lookupFields({
type: recordType,
id: recordId,
columns: ['tranid']
});
var transactionId = lookupResult.tranid;
log.debug('Transaction ID (lookup)', transactionId);
// Further processing using transactionId
}
return { afterSubmit: afterSubmit };
});Asynchronous Processing with Scheduled or Map/Reduce Scripts (SuiteScript 2.x):
If the immediate availability of the transaction ID is not critical, or if the subsequent processing is resource-intensive or involves large datasets, consider an asynchronous approach. This offloads the processing to a separate script execution, preventing delays in the user interface.
- Scheduled Script: For general asynchronous tasks, a SuiteScript 2.x scheduled script can be triggered to process the record after it has been fully committed. The
afterSubmitscript would simply enqueue the task for the scheduled script. - Map/Reduce Script: For processing large volumes of data, a Map/Reduce script is the recommended SuiteScript 2.x framework. The
afterSubmitscript could initiate a Map/Reduce job, passing the record ID for later processing.
- Scheduled Script: For general asynchronous tasks, a SuiteScript 2.x scheduled script can be triggered to process the record after it has been fully committed. The
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
