how many types of client script in servicenow

mshiraz826
Mega Explorer

Hi Family,

 

Can anyone tell me how many types of client script in ServiceNow 

 

Many Thanks,

Abel Tuter,

8 REPLIES 8

Dr Atul G- LNG
Tera Patron

Hi @mshiraz826 

 

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

 

Introduction to Client-side Scripting

Scripts in ServiceNow fall into two categories:

  • Client-side
  • Server-side

 

*************************************************************************************************************
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]

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

Dr Atul G- LNG
Tera Patron
  • 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 the form's appearance or content. Use sparingly to avoid impacting load times.
    • onChange: Executes when a specific field's value changes, useful for responding to user input and modifying other fields dynamically.
    • 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 not covered in detail in the referenced materials .
  • Best practices for client scripting include:
    • Use scripts primarily for data validation and user feedback.
    • Minimize the number and complexity of scripts to avoid slowing down form load times.
    • Enclose code in functions, minimize server lookups, and avoid unnecessary DOM manipulation.
    • Set the execution order of scripts when needed, and consider using UI Policies for simple field attribute changes instead of scripts.
    • Restrict list editing through appropriate controls if needed, as most client scripts apply only to forms .
  • If you need more specific guidance or examples, please specify your use case or the platform you are working with.
  • For further details, refer to:
    • Client Scripting Technical Best Practices 
    • Client Script Type
*************************************************************************************************************
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 @mshiraz826 

 

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

Tanushree Maiti
Tera Patron

Hi @mshiraz826 

 

Here All 4 client scripts with sample code as a example.

 

  1. Onload ()

Common Use Cases: Setting default field values, hiding or showing fields on load, or making fields read-only based on roles.

Code Example:

function onLoad() {

    if (g_form.isNewRecord()) {

                g_form.setValue('impact', '3');

               g_form.addInfoMessage('Welcome! Please fill out all necessary field details.');

    }

}

 

  1. onChange()

Common Use Cases: Making a field mandatory based on another choice, showing a section, or validating input format.

Code Example:

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

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

        return;

    }

    if (newValue == 'hardware') {

        g_form.setMandatory('u_serial_number', true);

        g_form.showFieldMsg('u_serial_number', 'Serial number is strictly required for hardware items.', 'info');

    } else {

        g_form.setMandatory('u_serial_number', false);

    }

}

 

3.onSubmit()

  • Common Use Cases: Final multi-field form validations or preventing submissions when formatting or security constraints are broken.

Code Example:

function onSubmit() {

    var shortDesc = g_form.getValue('short_description');

        if (shortDesc.length < 10) {

        g_form.addErrorMessage('Submission aborted: The short description must be at least 10 characters long.');       

        return false;

    }   

    return true;

}

  1. onCellEdit()
  • Common Use Cases: Restricting mass inline updates, validating list changes, or locking down specific columns based on user status.

Code Example:

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

    var saveAndClose = true;

    if (newValue == '7') {

         alert('Security Notice: Records cannot be Closed directly via the list view view. Please open the form.');

        saveAndClose = false;

    }

    callback(saveAndClose);

}

 

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