When a record needs automatic processing after it is saved, a User Event Script may be the right option. User Event Scripts can run during record actions such as create, edit, delete, or save, depending on how the script is written.
This is useful when business logic needs to happen automatically as part of the record lifecycle.
Scenario
A business team needs something to happen after a record is saved.
Examples may include:
- setting a follow-up field
- updating a related record
- creating a task
- writing an audit note
- validating record data
- sending information to another process
The user should not need to click another button or manually update another record after saving.
Solution
Use a User Event Script when logic should run automatically during a record event.
A User Event Script can be designed to run:
- before the record is saved
- after the record is saved
- when the record is loaded
For processing that should happen after the record is already saved, use the afterSubmit entry point.
When to Use a User Event Script
A User Event Script may be a good fit when:
- logic depends on a record being created or edited
- processing should happen automatically after save
- related records need to be updated
- validation must happen before saving
- users should not manually trigger the process
Example:
A custom request record is saved with a status of Pending Review. After save, the script creates a task for the reviewer.
When Not to Use a User Event Script
A User Event Script may not be the best option when:
- the process needs to run on a schedule
- large batches of records need processing
- users need a guided custom page
- logic should run only when a button is clicked
- processing should be handled asynchronously
In those cases, consider other script types such as Scheduled Scripts, Map/Reduce Scripts, Suitelets, or Client Scripts.
Simple Example
The sample below shows a User Event Script that runs after a custom record is saved. If the request status is Pending Review, it logs that the record is ready for follow-up.
/**
* @NApiVersion 2 .1
* @NScriptType UserEventScript
*/
define(['N/log'], (log) => {
function afterSubmit(context) {
const rec = context.newRecord;
const status = rec.getText({
fieldId: 'custrecord_request_status'
});
if (status === 'Pending Review') {
log.audit({
title: 'Record Ready for Review',
details: `Record ID ${rec.id} is pending review.`
});
}
}
return {
afterSubmit
};
});
How It Works
- afterSubmit runs after the record is saved.
- context.newRecord gets the saved record.
- getText reads the displayed status value.
- If the status is Pending Review, the script logs an audit message.
This sample is intentionally simple. In a real use case, the script might create a task, update a related record, or trigger another process.
Notes
- Use beforeSubmit when data must be changed before the record is saved.
- Use afterSubmit when processing should happen after the record is saved.
- Avoid heavy processing in User Event Scripts if the logic may slow down users.
- Use Scheduled or Map/Reduce Scripts for large batch processing.
- Test with create, edit, and delete scenarios where applicable.
- Confirm the script is deployed on the correct record type.
- Review execution logs during testing.
Disclaimer: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
Share your insights and experiences in the NetSuite Admin Corner.