onChange client script in servicenow

onChange client script will runs when a particular field value changes on the form.

An onChange script runs when the user changes value in any of the field. This script is handy for setting up a value of a field or displaying value based on the values user enters in the other fields. It runs not only on value change for already created records but also for adding value to newly created records.

I have explained the above pic in details in the video. Request you to watch it for better understanding.

 

In onChange() client script we will get below OOB parameters/Objects. Let me explain you each object in details.

A. control: the DHTML (Dynamic Hyper Text Markup Language) widget whose value changed.

      • Control parameters are generally used for If we wants to change the dynamically field size, length etc. in ui page. So we will use control parameter inside client script in onChange type. 
      • It’s basically the field for which this client script is written. And it will give you HTML objectfor that field so that you can change or use it as per your need for example restricting field length.
      • Name of the object (field_name) whose value just changed. The object is identified in the Field name field on the Client Script form.
      • To change the field value color we can use this script: style.color = ‘blue’;

 Note: control is not accessible in mobile and service portal. 

 

B. oldVaIue— Value of the control field when the form loaded and prior to the change. For example, if the value of Assigned to changes from Runjay to Suman, the value of the parameter oldValue is Runjay. oldVaIue will always be Runjay (the value when the form loaded) no matter how many times the Assigned to value changes after the original form load.

C. newVaIue— Value of the control field after the change. For example, if the value of Assigned to changes from Runjay to Suman, the value of the parameter newValue will be Suman.

D. isLoading— Boolean value indicating whether the change is occurring as part of a form load. Value is true if change is due to a form load. A form load means all of a form’s fields changed.

E. isTemplate— Boolean value indicating whether the change occurred due to population of the field by a template. Value is true if change is due to population from a template.

Now lets take a look on real word example

Real World Scenario

Requirement: Make Assignment group and assigned to field mandatory if state changes from New to In-Progress.

Solution: create onChange client script and write below code.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    if(newValue == 2){
        g_form.setMandatory('assignment_group', true);
        g_form.setMandatory('assigned_to', true);
    }else{
        g_form.setMandatory('assignment_group', false);
        g_form.setMandatory('assigned_to', false);
    }
   
}	

 

Q. Can you run onChange client script during form load?

Ans: The onChange client script runs when a selected field is changed on the form. It is important to note that an onChange script will also run when the form is loaded. This type of script is often used to auto-populate other fields on a form, based on data in the field the onChange script is running on.

You can not call event from client side, Event will be call on server side only but you can call event in UI action.
Q. Which is executed first UI or client script?
UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.

LEAVE A REPLY

Please enter your comment!
Please enter your name here