SuiteScript 2.x User Event Script Type

User event scripts are executed on the NetSuite server. They are executed when users perform certain actions on records, such as create, load, update, copy, delete, or submit. Most standard NetSuite records and custom record types support user event scripts. Exceptions include records used for personal identification purposes (such as a Driver’s License, Passport, or other Government-issued ID), some revenue recognition records, and some timecard-related records. See SuiteScript Supported Records for more information about specific records.

User event scripts can be used to perform the following tasks:

For additional information about SuiteScript 2.x User Event Scripts, see the following:

You can use SuiteCloud Development Framework (SDF) to manage user event scripts as part of file-based customization projects. For information about SDF, see SuiteCloud Development Framework. You can use the Copy to Account feature to copy an individual user event script to another of your accounts. Each user event script page has a clickable Copy to Account option in the upper right corner. For information about Copy to Account, see Copy to Account.

You can use SuiteScript Analysis to learn about when the script was installed and how it performed in the past. For more information, see Analyzing Scripts.

Also see the User Event Script Best Practices section in the SuiteScript Developer Guide for a list of best practices to follow when using user event scripts.

SuiteScript 2.x User Event Script Sample

The following sample shows a user event script. This script is designed for use in environments that do not use the Team Selling feature. Visit the SuiteScript 2.x Code Samples Catalog: User Event Script Samples for additional user event script samples.

When you deploy this script on the customer record, this script creates a follow-up phone call record for every newly created customer record.

Important:

Before running this script, you must replace the salesrep internal ID with one specific to your account. Specifically, use an ID that represents an employee who is classified as a sales rep. The sales rep option is located on the Human Resources subtab of the employee record. If you do not replace the ID, the script may not work as expected. Additionally, note that this script is designed to work in environments where the customer record includes a salesrep field. If the Team Selling feature is enabled, the customer record typically will not include a salesrep field.

For help with writing scripts in SuiteScript 2.x, see SuiteScript 2.x Hello World and SuiteScript 2.x Entry Point Script Creation and Deployment.

          /**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record'], function(record) {
    function beforeLoad(context) {
        if (context.type !== context.UserEventType.CREATE)
                return;
        var customerRecord = context.newRecord;
        customerRecord.setValue('phone', '555-555-5555');
        if (!customerRecord.getValue('salesrep'))
            customerRecord.setValue('salesrep', 46); // replace '46'  with one specific to your account
    }
    function beforeSubmit(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        var customerRecord = context.newRecord;
        customerRecord.setValue('comments', 'Please follow up with this customer!');

        if (!customerRecord.getValue('category')) {
            throw error.create({       // you can change the type of error that is thrown
                name: 'MISSING_CATEGORY',
                message: 'Please enter a category.'
            })
        }
    }
    function afterSubmit(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        var customerRecord = context.newRecord;
        if (customerRecord.getValue('salesrep')) {
            var call = record.create({
                type: record.Type.PHONE_CALL,
                isDynamic: true
            });
            call.setValue('title', 'Make follow-up call to new customer');
            call.setValue('assigned', customerRecord.getValue('salesrep'));
            call.setValue('phone', customerRecord.getValue('phone'));
            try {
                var callId = call.save();
                log.debug('Call record created successfully', 'Id: ' + callId);
            } catch (e) {
                log.error(e.name);
            }
        }
    }
    return {
        beforeLoad: beforeLoad,
        beforeSubmit: beforeSubmit,
        afterSubmit: afterSubmit
    };
}); 

        

Related Topics

SuiteScript 2.x Script Types
SuiteScript 2.x Bundle Installation Script Type
SuiteScript 2.x Client Script Type
SuiteScript 2.x Map/Reduce Script Type
SuiteScript 2.x Mass Update Script Type
SuiteScript 2.x Portlet Script Type
SuiteScript 2.x RESTlet Script Type
SuiteScript 2.x Scheduled Script Type
SuiteScript 2.x Suitelet Script Type
SuiteScript 2.x Workflow Action Script Type

General Notices