ANS-0387 · SUITESCRIPT DEVELOPMENT
How to Access Vendor Record Field Values in SuiteScript 2.x afterSubmit
Understand why direct record access in SuiteScript 2.x afterSubmit scripts can be unreliable and learn the correct method for retrieving persisted field data.
Short answer
In SuiteScript 2.x, the newRecord object in an afterSubmit user event script is read-only. To reliably access field values from the persisted record, especially for fields that might be modified by other scripts or workflows during submission, use N/search.lookupFields or record.lookupFields to retrieve the data directly from the saved record.
Scenario
When attempting to retrieve field values from a Vendor record within an afterSubmit user event script in NetSuite, direct access using record.getValue() on the context.newRecord object may not always provide the most up-to-date or reliable persisted data. This occurs because the newRecord object in this context is read-only and represents the record as it was submitted, not necessarily its final state after all system processes and other scripts have run.
Solution
To ensure accurate retrieval of field values from a Vendor record (or any record type) in an afterSubmit user event script, it is recommended to perform a lookup on the persisted record rather than relying solely on the read-only context.newRecord object. This approach guarantees that the values retrieved reflect the record's state after it has been fully saved and processed.
Obtain the internal ID of the record from context.newRecord.
Use N/search.lookupFields (or record.lookupFields if the record type is known and you have the record ID) to query the persisted record for the desired field values.
Example using N/search.lookupFields:
javascript
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/search', 'N/record'], function(search, record) {
function afterSubmit(context) {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var vendorId = context.newRecord.id;
var vendorType = context.newRecord.type;
// Lookup specific fields from the persisted record
var lookedUpFields = search.lookupFields({
type: vendorType,
id: vendorId,
columns: ['entityid', 'companyname', 'email'] // Replace with actual field IDs
});
var vendorName = lookedUpFields.entityid;
var companyName = lookedUpFields.companyname;
var vendorEmail = lookedUpFields.email;
// Further processing with the retrieved values
// log.debug('Vendor Name', vendorName);
// log.debug('Company Name', companyName);
// log.debug('Vendor Email', vendorEmail);
}
}
return {
afterSubmit: afterSubmit
};
});This method ensures that the script accesses the most current and reliable data for post-submission processing.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
