Understanding NetSuite Client Scripts: A Simple Guide

Understanding NetSuite Client Scripts: A Simple Guide

Client scripts in NetSuite are a great way to customize how records behave in the user interface. They run directly in the browser, meaning they do not work when a record is modified through SuiteScript or SuiteTalk. Since they use the browser’s JavaScript engine, you can easily debug them using console.log.

In this article, we’ll break down client scripts in simple terms, covering their main functions and how they interact with records.

What Can Client Scripts Do?

Client scripts are typically deployed on specific record types. They can also be attached to Suitelets. However, there are some limitations:

  • They cannot add or remove fields from the UI but can hide or show them.
  • They cannot add or remove buttons but can control their behavior.

Now, let’s dive into the main entry points that make client scripts work.


Key Entry Points in Client Scripts

1. pageInit: Runs When the Record Loads

This function is triggered when a record opens in the UI. It is mandatory in every client script, even if it does nothing.

Example:

define([], () => {

    const pageInit = (scriptContext) => {
      // Initialization logic goes here
    }

    return {
        pageInit
    }
});        

The scriptContext object provides:

  • scriptContext.currentRecord – The current record in the UI.
  • scriptContext.mode – The mode of the record (create, copy, or edit).


2. validateField: Validates Field Input

This function runs when a user changes a field. It ensures that the entered value is valid. If the function returns false, the change is rejected.

However, note that validateField does not directly provide the new value. You need to use getValue({fieldId}) to retrieve it.

Example:

const validateField = (scriptContext) => {
    const currentRecord = scriptContext.currentRecord;
    const fieldId = scriptContext.fieldId;

    const newValue = currentRecord.getValue({ fieldId });

    if (fieldId === 'custrecord_amount' && newValue < 0) {
        alert('Amount cannot be negative.');
        return false;
    }
    
    return true;
}        

3. fieldChanged: Runs After a Field Update

This function triggers after a field is changed and saved. It does not return a value but is useful for running actions when specific fields are modified.

Example:

const fieldChanged = (scriptContext) => {
    if (scriptContext.fieldId === 'custrecord_status') {
        console.log('Status field changed!');
    }
}        

4. PostSourcing: Runs After Linked Fields Are Populated

When a field automatically fills related data (e.g., selecting a customer on a sales order auto-fills other fields), this function is triggered after the dependent fields are updated.

5. sublistChanged: Detects Sublist Changes

This function tracks changes in sublists, such as when a new line is inserted, deleted, or committed.

Example:

const sublistChanged = (scriptContext) => {
    console.log(`Sublist ${scriptContext.sublistId} changed with operation: ${scriptContext.operation}`);
}        

6. validateLine: Validates a Sublist Line

This function ensures that a sublist line is valid before it’s committed. If it returns false, the line is not saved.

Example:

const validateLine = (scriptContext) => {
    const currentRecord = scriptContext.currentRecord;
    
    const totalAmount = currentRecord.getCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'amount'
    });

    if (totalAmount < 0) {
        alert('Total amount cannot be negative.');
        return false;
    }

    return true;
}        

7. validateDelete & validateInsert: Controlling Sublist Actions

  • validateDelete prevents a sublist row from being deleted if it returns false.
  • validateInsert prevents users from inserting a new row if it returns false.

Example:

const validateInsert = (scriptContext) => {
    alert('New rows cannot be added.');
    return false;
}        

Conclusion

Client scripts are a powerful way to enhance the NetSuite UI and automate actions for users. However, they have limitations and should be used wisely. Complex validations and backend processing should still be handled in user event scripts.

If you're new to client scripts, start small—experiment with console.log and simple field changes to see how they work. Once comfortable, you can build more advanced automation to improve user experience and data accuracy in NetSuite.

To view or add a comment, sign in

More articles by Dilshad Ali

  • Easy Guide to String Replacement in JavaScript

    String replacement is a common task in JavaScript. Whether you’re cleaning text, updating user input, or modifying…

  • Top 10 JavaScript Tips & Tricks for Efficient Coding

    JavaScript is a versatile and powerful language that powers the modern web. Mastering a few key techniques can…

  • Wiladat-e-Fatema Zehra (a.s)

    In The Name Of Allah, The Most Compassionate, The Merciful O Allah! Send your blessings to the head of your Messengers…

Others also viewed

Explore content categories