Create a User Event Script

This topic is step five of the SuiteCloud Development Framework Tutorial. If you have not already done so, complete step four: Create a Custom Record Definition.

User event scripts are executed on the NetSuite server when users perform certain actions, such as create, load, update, copy, or delete. In this step, you create a user event script that sets the value of the custom memo field each time a user loads the SuiteCloud Development Framework (SDF) Tutorial record in the UI. For more information about user event scripts, see SuiteScript 2.x User Event Script Type and SuiteScript 2.x User Event Script Entry Points and API.

The following steps show you how to create a user event script file. Depending on your IDE, refer to one of the following procedures:

To create a user event script in Visual Studio Code

  1. With the SDFTutorial open in Visual Studio Code, press Ctrl+Shift+P to open the Command Palette and enter suitecloud. Then from the dropdown list, select SuiteCloud: Create SuiteScript File.

  2. For the type of SuiteScript file to create, select User Event Script.

  3. Without any modules selected, click OK to go to the next step.

  4. Save the file in the following folder location: /SuiteScripts.

  5. Enter the following name for the SuiteScript file: UserEventScript.js.

    The SuiteScript file is created in the specified folder location and opened for editing in Visual Studio Code.

  6. In the beforeLoad function, enter the following code:

                    function beforeLoad(scriptContext) {
        if (scriptContext.type === scriptContext.UserEventType.CREATE) {
            var  customRecord = scriptContext.newRecord;
            customRecord.setValue({
                fieldId: 'custrecord_tut_memo',
                value: 'My first account customization project'
            });
        }
    } 
    
                  

    The code above sets a value for the custrecord_tut_memo text field before the SDF Tutorial record type loads. For more information about setting record values, see record.Record and Record.setValue(options).

  7. Save the file.

To create a user event script in WebStorm

  1. Select FileCabinet > SuiteScripts within the SDFTutorial project, and select File > New > SuiteScript File.

  2. In the New SuiteScript File dialog, do the following:

    • From the Type dropdown list, select User Event Script.

    • In the Name field, enter UserEventScript.

  3. Click OK.

    The user event script file is created based on a template that contains user event script entry points.

  4. In the beforeLoad function, enter the following code:

                    function beforeLoad(scriptContext) {
        if (scriptContext.type === scriptContext.UserEventType.CREATE) {
            var  customRecord = scriptContext.newRecord;
            customRecord.setValue({
                fieldId: 'custrecord_tut_memo',
                value: 'My first account customization project'
            });
        }
    } 
    
                  
  5. Your code should look like the following:

                    /**
     * @NApiVersion 2.x
     * @NScriptType UserEventScript
     * @NModuleScope SameAccount
     */
    define([],
    
        function() {
    
            /**
             * Function definition to be triggered before record is loaded.
             *
             * @param {Object} scriptContext
             * @param {Record} scriptContext.newRecord - New record
             * @param {string} scriptContext.type - Trigger type
             * @param {Form} scriptContext.form - Current form
             * @Since 2015.2
             */
            function beforeLoad(scriptContext) {
                if (scriptContext.type === scriptContext.UserEventType.CREATE) {
                    var  customRecord = scriptContext.newRecord;
                    customRecord.setValue({
                        fieldId: 'custrecord_tut_memo',
                        value: 'My first account customization project'
                    });
                }
            }
    
            /**
             * Function definition to be triggered before record is loaded.
             *
             * @param {Object} scriptContext
             * @param {Record} scriptContext.newRecord - New record
             * @param {Record} scriptContext.oldRecord - Old record
             * @param {string} scriptContext.type - Trigger type
             * @Since 2015.2
             */
            function beforeSubmit(scriptContext) {
    
            }
    
            /**
             * Function definition to be triggered before record is loaded.
             *
             * @param {Object} scriptContext
             * @param {Record} scriptContext.newRecord - New record
             * @param {Record} scriptContext.oldRecord - Old record
             * @param {string} scriptContext.type - Trigger type
             * @Since 2015.2
             */
            function afterSubmit(scriptContext) {
    
            }
    
            return {
                beforeLoad: beforeLoad,
                beforeSubmit: beforeSubmit,
                afterSubmit: afterSubmit
            };
    
        }); 
    
                  

    The code above sets a value for the custrecord_tut_memo text field before the SDF Tutorial record type loads. For more information about setting record values, see record.Record and Record.setValue(options).

  6. Save the file.

Continue to Step Six: Create a Script Record and Script Deployment Definition.

Related Topics

SuiteCloud Development Framework Tutorial
Set Up a NetSuite Account (Administrator Only)
Set Up Your IDE Environment
Create an Account Customization Project
Create a Custom Record Definition
Create a Script Record and Script Deployment Definition
Validate and Deploy the SuiteCloud Project to a Target NetSuite Account
Test the SuiteCloud Project in the Target NetSuite Account

General Notices