Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The NetSuite Pro

The NetSuite Pro Logo The NetSuite Pro Logo

The NetSuite Pro Navigation

  • Home
  • About Us
  • Tutorials
    • NetSuite Scripting
    • Advanced PDF Templates in NetSuite
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About Us
  • Tutorials
    • NetSuite Scripting
    • Advanced PDF Templates in NetSuite
  • Blog
  • Contact Us
Home/ NetSuite Scripting/User Event Script Basics in NetSuite (SuiteScript 2.1 Guide)

User Event Script Basics in NetSuite (SuiteScript 2.1 Guide)

🔹 Introduction

User Event (UE) Scripts are server-side scripts that run on record actions: when they load, before saving, and after saving. They’re essential for enforcing business rules, validating data, and automating processes.

Each code block below includes a try–catch structure so you can see how to log and handle errors effectively.


🔹 Example 1: Add Default Memo on BeforeLoad

What this script does:

  • Auto-fills the Memo field with default text when a new Sales Order is created.
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define([], () => {
    const beforeLoad = (context) => {
        try {
            if (context.type === context.UserEventType.CREATE) {
                const rec = context.newRecord;
                rec.setValue({
                    fieldId: 'memo',
                    value: 'Generated by User Event Script'
                });
                log.debug('Success', 'Default memo set on new Sales Order');
            }
        } catch (e) {
            log.error('Error in beforeLoad', e.message);
        }
    };
    return { beforeLoad };
});

🔹 Example 2: Validate Required Field Before Submit

What this script does:

  • Blocks saving a Customer if Email is missing.
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define([], () => {
    const beforeSubmit = (context) => {
        try {
            if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
                const rec = context.newRecord;
                const email = rec.getValue('email');
                
                if (!email) {
                    throw Error('Email address is required before saving this customer.');
                }
                log.debug('Validation Passed', 'Email is present.');
            }
        } catch (e) {
            log.error('Error in beforeSubmit', e.message);
            throw e; // rethrow to stop record save
        }
    };
    return { beforeSubmit };
});

🔹 Example 3: Send Email After Record Save

What this script does:

  • When a new Customer is created, it emails the admin.
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define(['N/email', 'N/runtime'], (email, runtime) => {
    const afterSubmit = (context) => {
        try {
            if (context.type === context.UserEventType.CREATE) {
                const rec = context.newRecord;
                const customerName = rec.getValue('companyname');
                
                email.send({
                    author: runtime.getCurrentUser().id,
                    recipients: 'admin@company.com',
                    subject: 'New Customer Created',
                    body: `A new customer named ${customerName} has been added.`
                });
                log.debug('Success', `Email sent for new customer: ${customerName}`);
            }
        } catch (e) {
            log.error('Error in afterSubmit (email)', e.message);
        }
    };
    return { afterSubmit };
});

🔹 Example 4: Update Related Record After Submit

What this script does:

  • When a Sales Order is created, it updates a custom checkbox on the related Customer record.
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define(['N/record'], (record) => {
    const afterSubmit = (context) => {
        try {
            if (context.type === context.UserEventType.CREATE) {
                const so = context.newRecord;
                const customerId = so.getValue('entity');
                
                if (customerId) {
                    const custRec = record.load({
                        type: record.Type.CUSTOMER,
                        id: customerId
                    });
                    
                    custRec.setValue({
                        fieldId: 'custentity_has_salesorder',
                        value: true
                    });
                    custRec.save();
                    log.debug('Success', `Customer ${customerId} updated with has_salesorder flag.`);
                }
            }
        } catch (e) {
            log.error('Error in afterSubmit (update customer)', e.message);
        }
    };
    return { afterSubmit };
});

🔹 Best Practices

  • Always wrap logic in try–catch and use log.error() to capture failures.
  • Use beforeSubmit for validations that must block saving.
  • Use afterSubmit for downstream actions (emails, integrations, updates).
  • Keep heavy logic out of UE scripts → use Scheduled or Map/Reduce for large jobs.

✅ Key Takeaway

User Event Scripts let you:

  • beforeLoad → Modify form before display
  • beforeSubmit → Validate before database commit
  • afterSubmit → Automate after save

By adding try–catch and logging, you’ll make scripts more robust and easier to debug.

Share
  • Facebook

Sidebar

Ask A Question

Stats

  • Questions 6
  • Answers 6
  • Best Answers 0
  • Users 2
  • Popular
  • Answers
  • Rocky

    Issue in running a client script in NetSuite SuiteScript 2.0 ...

    • 1 Answer
  • admin

    How can I send an email with an attachment in ...

    • 1 Answer
  • admin

    How do I avoid SSS_USAGE_LIMIT_EXCEEDED in a Map/Reduce script?

    • 1 Answer
  • admin
    admin added an answer The issue is usually caused by following Wrong script file… September 14, 2025 at 10:33 pm
  • admin
    admin added an answer Steps to send an Invoice PDF by email: define(['N/email', 'N/render',… August 28, 2025 at 3:05 am
  • admin
    admin added an answer This error means your script hit NetSuite’s governance usage limit… August 28, 2025 at 3:02 am

Top Members

Rocky

Rocky

  • 1 Question
  • 21 Points
Begginer
admin

admin

  • 5 Questions
  • 2 Points

Trending Tags

clientscript netsuite scripting suitescript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

© 2025 The NetSuite Pro. All Rights Reserved