ANS-1531 · SUITESCRIPT DEVELOPMENT

How to Access the Old Record in NetSuite AfterSubmit Scripts?

Understand when and how to retrieve previous record data using scriptContext.oldRecord in SuiteScript 2.x afterSubmit event handlers.

Short answer

In NetSuite SuiteScript 2.x, scriptContext.oldRecord is available within an afterSubmit script, providing access to the record's state before submission. It is not available during a 'create' event, as no prior record exists. Its behavior in 'xedit' scenarios may vary.

Scenario

NetSuite developers often need to compare the current state of a record with its previous state after a submission event. This is crucial for auditing changes, triggering conditional logic based on modifications, or integrating with external systems. The challenge lies in reliably accessing the 'old' record data within an afterSubmit user event script.

Solution

Within a NetSuite SuiteScript 2.x afterSubmit user event script, the previous version of a record can be accessed using scriptContext.oldRecord. This object provides a snapshot of the record's fields and sublists as they were before the current submission.The availability and behavior of scriptContext.oldRecord are subject to the following conditions:

  1. It is not available during a 'create' event, as there is no prior record state to retrieve.

  2. Its behavior in 'xedit' (inline edit) situations may not always provide the expected full old record data, potentially reflecting only the fields that were directly modified.For developers still utilizing SuiteScript 1.0, the function nlapiGetOldRecord() offers equivalent functionality under the same conditions.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant