In the last tutorial we looked at the setup of the Before Load trigger for User Event scripts. In this tutorial we will introduce the basic setup of the 2nd of three main User Event triggers: the Before Submit trigger. I want to spend some time looking at each of these triggers before jumping into a sample script project since it is important to have a good basic understanding of these triggers.
Before Submit Trigger
The beforeSubmit function in User Event Scripts triggers when a user saves a record but before the record information submits to the database. You may want to use this trigger at times when you need to make last-minute field changes to the record before the record is actually submitted.
Here are some examples of what you can do with the beforeSubmit function in a User Event Script.
- When creating and saving an item, automatically set a custom stock number.
- When saving a transaction, automatically set a custom field based on line item calculations.
- If you are creating and saving a transaction, automatically look up the primary contact from the customer record and enter it in a custom field.
User Event Basic Structure
In the last tutorial we put together the basic structure of a User Event Script. Here is what we put together:
define(['N/record', 'N/search'], function (record, search) {
/**
*@NApiVersion 2.0
*@NScriptType UserEventScript
*/
//everything else will go inside here
});
Before Submit Basic Structure
You can add the beforeSubmit
trigger to the script by adding the beforeSubmit
function and then adding it to the return section of the script. Here is what the basic beforeSubmit
function looks like:
function beforeSubmit(context) {
//all your Before Submit actions will go in here.
}
And the return section will look like this:
return {
beforeSubmit: beforeSubmit
}
When you add that to the rest of the script, it will look like this:
define(['N/record', 'N/search'], function (record, search) {
/**
*@NApiVersion 2.0
*@NScriptType UserEventScript
*/
function beforeSubmit(context) {
//all your Before Submit actions will go in here.
}
return {
beforeSubmit: beforeSubmit
}
});
If you need to use multiple triggers in a User Event script, this is what your code will look like. This includes the beforeLoad
and beforeSubmit
triggers:
define(['N/record', 'N/search'], function (record, search) {
/**
*@NApiVersion 2.0
*@NScriptType UserEventScript
*/
function beforeLoad(context) {
//all your Before Load actions will go in here.
}
function beforeSubmit(context) {
//all your Before Submit actions will go in here.
}
return {
beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit
}
});
Conclusion
If this blog was helpful, be sure to check out the next blog in this series covering User Event Script After Submit. You could also check out the NetSuite help docs on User Event execution here. And don’t forget to subscribe below to receive more NetSuite development blogs directly in your inbox!