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. Make field read only when state = Resolved.
ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
// Stop script running on form load
if (isLoading) return;
// Get current state value
var state = g_form.getValue('state');
// Lock fields when incident is resolved
if (state === '6') { // 6 = Resolved
g_form.setReadOnly('short_description', true);
g_form.setReadOnly('description', true);
g_form.setReadOnly('category', true);
g_form.addErrorMessage('Incident is Resolved — fields are locked');
} else {
g_form.setReadOnly('short_description', false);
g_form.setReadOnly('description', false);
g_form.setReadOnly('category', false);
g_form.clearMessages();
}
}
7. Auto-populate field based on Priority value.
ServiceNow Code
function onChange(control, oldValue, newValue, isLoading) {
// Stop script running on form load
if (isLoading) return;
// Get the priority value user just selected
var priority = g_form.getValue('priority');
// Auto-set Impact based on Priority
if (priority === '1') {
g_form.setValue('impact', '1'); // High impact
g_form.setValue('urgency', '1'); // High urgency
g_form.addInfoMessage('P1 Incident — Notify Manager immediately');
} else if (priority === '2') {
g_form.setValue('impact', '2'); // Medium impact
g_form.setValue('urgency', '2'); // Medium urgency
} else {
g_form.clearMessages(); // Clear banner messages
}
}