On Change Client Script In ServiceNow

OnChange():

    Runs when a field value changes.



1. Control (Field Name)

  • This tells the script which field on the form just changed.
  • It is the name of the object whose value was updated.
  • Example — if the "Assigned To" field changed, then Control = assigned_to.
  • You define this field name inside the Client Script form itself.
2. OldValue (Value Before Change)
  • This is the value the field had when the form first loaded.
  • It stays the same no matter how many times the field changes after loading.
  • Example — if Assigned To changed from Jai → Abhijeet, then OldValue = Jai.
  • Even if you later change it again to someone else, OldValue will always remain Jai.
3. NewValue (Value After Change)
  • This is the latest value of the field after the change happened.
  • It updates every time the user makes a new selection or input.
  • Example — if Assigned To changed from Jai → Abhijeet, then NewValue = Abhijeet.
  • Your script can use this to trigger actions based on the new value.
4. IsLoading (Is the Form Still Loading?)
  • This is a True / False value.
  • True = the field changed because the form is still loading — not because the user did something.
  • False = the user manually changed the field after the form fully loaded.
  • Very useful to avoid running unnecessary actions during form load.
  • Example — you don't want an alert to pop up every time the form opens, so you check IsLoading = false first.
5. IsTemplate (Did a Template Fill This Field?)
  • This is also a True / False value
  • True = the field was filled automatically by a pre-defined template, not the user
  • False = the user changed the field manually themselves
  • Helps your script understand why the value changed — user action or template action
  • Example — if a template auto-fills the "Priority" field, IsTemplate = true

Parameters:

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

Use Cases: 

    ● Auto-fill fields 

    ● Validate field values 

    ● Trigger logic based on selection 

1. Set Value to Priority 1.

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if (newValue == '1') {
    g_form.setValue('priority', '1');
  }
}

2. Make Field Mandatory Based on Priority .

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if(newValue == '1'){ 
        g_form.setMandatory('description', true);
    } }

3. Hide Field Based on Category.

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if(newValue == 'software'){ 
        g_form.setDisplay('subcategory', false);
    }
}

4.  Validate Field Length.

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if(newValue.length < 10){ 
        g_form.showFieldMsg('description','Minimum 10 characters required','error');
    } }

5. Auto Set Assignment Group.

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if(newValue == 'network'){ 
        g_form.setValue('assignment_group','Network Team');
    } }

6. Set Value to Pripority 1.

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if (newValue == '1') {
    g_form.setValue('priority', '1');
  }
}

7. Set Value to Pripority 1.

ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if (newValue == '1') {
    g_form.setValue('priority', '1');
  }
}

Post a Comment

0Comments
Post a Comment (0)