In the last tutorial we looked at the setup of the Before Load trigger for User Event scripts. In this tutorial will introduce the basic setup of the 2nd of 3 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 are triggered when a record is saved but before the record information is submitted 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 can be done with the beforeSubmit funciton in a User Event Script.

  • When an item is created and saved, automatically set a custom stock number.
  • When a transaction is saved, automatically set a custom field based on line item calculations.
  • when a transaction is created and saved, 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 sctructure 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

The beforeSubmit trigger can be added 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
}
});

Check out other blogs in our NetSuite Development series and subscribe below to never miss another post!

Using an Enums Custom Library, Part 1

In a previous developer post, we introduced a new paradigm for structuring SuiteScript files in a project.As we address some of the details of this structure, we come to the topic of enums. Enums, short for "enumerated types" or "enumerations," is basically a separate...

Scripting Pattern – Client Script Calling a Suitelet

In NetSuite development, there are a few common scripting patterns to master. One of these patterns is a kind of automation that connects both the client side and a server side. Today we will be looking at a Client Script calling a Suitelet. In the previous NetSuite...

SuiteScript Code Structure

Having a standard method for your SuiteScript code structure can be extremely helpful. Having a set structure provides consistency, preemptively removes difficult roadblocks, and reduces the number and severity of bugs. A Structural Overview This is a cutting-edge...

SuiteScript Naming Conventions

SuiteScript naming conventions can prevent NetSuite from becoming messy and help you locate your scripts in NetSuite efficiently. A Quick Blog Roadmap If you have been following along with our SuiteScript blog, we've covered a lot of ground! We have learned about the...

Handle POST Request in a Suitelet (Build a Suitelet, Part 5)

Once we design our Suitelet to display helpful information, how do we handle interactions with the Suitelet by a user? In this blog post we will learn how to handle new information posted to the server to perform important tasks. If you're new to this mini-series...
0
Would love your thoughts, please comment.x
()
x