SuiteScript 2.x User Event Script Type

User event scripts run on the NetSuite server whenever you create, load, update, copy, delete, or submit a record. Most standard NetSuite records and custom record types support user event scripts, except for records like personal ID (such as Driver’s Licenses and Passports), some revenue recognition records, and some timecard-related records. For details on which records are supported, see SuiteScript Supported Records.

You can use user event scripts to do things like:

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 in file-based customization projects. For more about SDF, see SuiteCloud Development Framework. You can also use Copy to Account to copy a user event script to another account. Click Copy to Account at the top right of the script page. For details, see Copy to Account.

Use SuiteScript Analysis to see when a script was installed and how it performed in the past. To learn more, see Analyzing Scripts.

For tips, check out the User Event Script Best Practices in the SuiteScript Developer Guide.

SuiteScript 2.x User Event Script Sample

Here’s a sample user event script for environments without the Team Selling feature. For more samples, see the SuiteScript 2.x Code Samples Catalog: User Event Script Samples.

When you deploy this script on a customer record, it creates a follow-up phone call record each time a new customer is added.

Important:

Before running this script, make sure you replace the salesrep internal ID with one that matches your account—use an ID for an employee marked as a sales rep. You’ll find this option on the Human Resources subtab of the employee record. If you don’t update the ID, the script might not work. This script is made for environments where customer records have a salesrep field. If Team Selling is enabled, customer records usually won’t include a salesrep field.

For help with 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

General Notices