How Many types of client scripts in ServiceNow

UdayG0047661101
Kilo Explorer

Hi Family,

 

Can anyone tell me How Many types of client scripts in ServiceNow?

 

Many Thanks,

Abel Tuter

6 REPLIES 6

vaishali231
Kilo Sage

Hey @UdayG0047661101 

In ServiceNow, Client Scripts are used to perform actions on forms in the user's browser without requiring a server call. They help improve user experience by validating data, controlling field behavior, and responding to user interactions.

There are 4  types of Client Scripts:

1. OnLoad Client Script
Purpose:

Runs automatically when a form is loaded.

Common Use Cases:

  1. Make fields read-only
  2. Hide or show fields
  3. Set default values
  4. Display informational messages

Structure:

function onLoad() {

    // Code executes when form loads

}

 

Example:

function onLoad() {

    g_form.setReadOnly('priority', true);

    g_form.addInfoMessage('Welcome to the Incident form.');

}


2. OnChange Client Script
Purpose
:
Runs whenever the value of a specified field changes.

Common Use Cases:

  1. Dynamic field validation
  2. Auto-populating fields
  3. Showing or hiding fields based on selections

Structure:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

 

    if (isLoading || newValue === '') {

        return;

    }

 

    // Your logic here

}

 

Example:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == '1') {

        g_form.showFieldMsg('priority',

            'High Priority selected',

            'info');

    }

}

 

3. OnSubmit Client Script
Purpose
:
Runs when the user clicks Submit, Update, Save, Order Now, etc.

Common Use Cases:

  1. Form validation before submission
  2. Preventing record save when required conditions are not met

Structure:

function onSubmit() {

 

    // Validation logic

 

    return true;  // Allow submission

}

 

Example:

function onSubmit() {
    if (g_form.getValue('short_description') == '') {
        g_form.addErrorMessage(
            'Short Description is mandatory.'
        );
        return false;
    }
    return true;
}

4 . OnCellEdit Client Script
Purpose
:
Runs when a cell value is edited in a List View.

Common Use Cases:

  1. Validate list edits
  2. Restrict invalid updates in list editing

Structure:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

 

    // Validation logic

 

    callback(true);

}

 

Example:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

    if (newValue == " '') {
        alert('Value cannot be empty');
        callback(false);
        return;
    }
    callback(true);
}

Best Practice:

  1. Use Client Scripts only for UI-related logic.
  2. Avoid heavy processing on the client side.
  3. Use GlideAjax when server-side data is required.
  4. Keep scripts lightweight for better form performance.

Official documentation also provides detailed guidance on Client Script types, execution order, and best practices within the platform.

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb




BharatC
Mega Guru
servicenow #skfacts #servicenowdeveloper #ServiceNow #ITSM #ITIL #ServiceManagement #ServiceDesk #IncidentManagement #ChangeManagement #CMDB #Automation #DigitalTransformation #ServiceNowAdmin #ServiceNowDevelopment #ServiceNowCommunity #ServiceNowTraining #ServiceNowIntegration students can log ...
servicenow #coding #interviewquestions #interview 1. What is the parent table of Incident and Change applications ? - task 2. What are the Applications you see in ITSM? - incident change problem request/task knowledge 3. What is Dictionary? What is Dictionary override ? sys_dictionary. 4. What is

Dr Atul G- LNG
Tera Patron

Hi @UdayG0047661101 

 

https://developer.servicenow.com/dev.do#!/learn/courses/australia/app_store_learnv2_scripting_austra...

  • A Client Script is JavaScript code that runs on the client side, typically within forms, to enhance user experience and validate data before submission .
  • There are several types of client scripts:
    • onLoad: Executes when a form is loaded, often used to manipulate form appearance or content. Use sparingly to avoid impacting load times.
    • onChange: Executes when a specific field's value changes, allowing dynamic responses such as making other fields mandatory or displaying alerts.
    • onSubmit: Executes when a form is submitted, commonly used for validating field values and preventing invalid data from being saved.
    • onCellEdit: Applies to lists rather than forms and is used for handling cell edits in list views .
  • Best practices include running only necessary scripts, enclosing code in functions, minimizing server lookups, and using UI Policies instead of scripts for setting field attributes when possible. Always validate user input and set the execution order of scripts to optimize performance .
  • Avoid unnecessary DOM manipulation and ensure scripts are efficient to prevent slow form load time

 

https://developer.servicenow.com/dev.do#!/ai-search/client%20script

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Tanushree Maiti
Tera Patron

Hi @UdayG0047661101 

 

In ServiceNow, there are four main types of Client Scripts. Each type is triggered by a specific user action or browser-side event and is used to enhance the user experience on forms and lists.

  1. onLoad()

Trigger: Executes when a form finishes loading in the browser.

Common Use Cases:

  • Setting default field values.
  • Showing or hiding fields and sections.
  • Making fields read-only or mandatory based on conditions such as user roles.
  • Initializing form behavior when the record is opened.

 

  1. onChange()

Trigger: Executes when the value of a specified field changes.

Common Use Cases:

  • Auto-populating related fields.
  • Displaying validation messages or alerts.
  • Dynamically showing, hiding, enabling, or disabling fields based on user selections.
  • Performing client-side validation.

Note: The isLoading parameter is commonly used to prevent the script from running while the form is still loading.

 

  1. onSubmit()

Trigger: Executes when a user attempts to save, submit, or update a form.

Common Use Cases:

  • Performing final validation checks before data is sent to the server.
  • Ensuring required business conditions are met.
  • Displaying confirmation messages or preventing invalid submissions.

Note: Returning false from an onSubmit Client Script prevents the form from being submitted.

 

  1. onCellEdit()

Trigger: Executes when a user edits a field directly within a list view using inline editing.

Common Use Cases:

  • Validating changes made from a list.
  • Enforcing data integrity rules during inline edits.
  • Controlling or restricting bulk updates.

Note: This script type uses parameters such as sysIDs, table, oldValues, newValue, and a callback function to validate and approve or reject the change.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti