What are Client Scripts?

Client Scripts run in the user's browser and control form behavior. They execute when forms are loaded, when field values change, when forms are submitted, or when list cells are edited.

Important

Client Scripts run on the CLIENT side (browser), not on the server. They cannot directly query the database - use GlideAjax for that.

Four Types of Client Scripts

Type When It Runs Use Case
onLoad When form loads Set default values, make fields mandatory/readonly
onChange When field value changes Field validation, populate related fields
onSubmit Before form submission Form validation, prevent submission
onCellEdit When list cell is edited Validate list inline edits

onLoad Client Scripts

Execute when a form is loaded and the DOM is ready.

onLoad Example - Set Field Mandatory
function onLoad() {
    // Make assignment_group mandatory for high priority incidents
    if (g_form.getValue('priority') == '1') {
        g_form.setMandatory('assignment_group', true);
        g_form.showFieldMsg('assignment_group', 'Required for P1 incidents', 'info');
    }
    
    // Set default value if empty
    if (g_form.getValue('category') == '') {
        g_form.setValue('category', 'inquiry');
    }
    
    // Hide/Show sections
    if (g_form.getValue('state') == '6') {  // Resolved
        g_form.setSectionDisplay('resolution_info', true);
    }
}
Best Practice

Keep onLoad scripts lightweight. Heavy operations can slow down form loading. Avoid database queries in onLoad scripts.

onChange Client Scripts

Execute when a specific field value changes. Must specify which field triggers the script.

onChange Example - Priority Field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Don't run during form load or templates
    if (isLoading || isTemplate) {
        return;
    }
    
    // Check if value actually changed
    if (oldValue == newValue) {
        return;
    }
    
    // Get the field name
    var fieldName = control.id;
    
    // Business logic based on priority
    if (newValue == '1') {  // Critical
        g_form.setMandatory('assignment_group', true);
        g_form.setMandatory('assigned_to', true);
        g_form.showFieldMsg('priority', 'Critical incident - immediate assignment required', 'error');
    } else {
        g_form.setMandatory('assignment_group', false);
        g_form.setMandatory('assigned_to', false);
        g_form.hideFieldMsg('priority');
    }
}
onChange Example - Category/Subcategory
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || isTemplate) {
        return;
    }
    
    // Clear subcategory when category changes
    if (oldValue != newValue) {
        g_form.clearValue('subcategory');
        
        // Set filter for subcategory based on category
        g_form.addFilter('subcategory', 'category', newValue);
    }
}

onSubmit Client Scripts

Execute when form is submitted. Can prevent submission by returning false.

onSubmit Example - Validation
function onSubmit() {
    // Validate high priority incidents
    var priority = g_form.getValue('priority');
    var assignmentGroup = g_form.getValue('assignment_group');
    
    if (priority == '1' && assignmentGroup == '') {
        g_form.addErrorMessage('Critical incidents must have an assignment group');
        return false;  // Prevent submission
    }
    
    // Validate resolved incidents have resolution notes
    var state = g_form.getValue('state');
    var resolutionNotes = g_form.getValue('close_notes');
    
    if (state == '6' && resolutionNotes == '') {
        var confirmed = confirm('No resolution notes provided. Continue anyway?');
        return confirmed;  // Only submit if user confirms
    }
    
    return true;  // Allow submission
}
Best Practice

Use g_form.addErrorMessage() to show specific error messages. Always return true or false from onSubmit scripts.

onCellEdit Client Scripts

Execute when a cell is edited in a list view with inline editing enabled.

onCellEdit Example
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    // Validate the new value
    if (newValue == '' || newValue == null) {
        alert('Value cannot be empty');
        callback(false);  // Reject the edit
        return;
    }
    
    // Additional validation
    if (table == 'incident' && newValue == '1') {  // Priority 1
        var confirmMsg = 'Setting priority to Critical for ' + sysIDs.length + ' record(s). Continue?';
        if (!confirm(confirmMsg)) {
            callback(false);  // Reject the edit
            return;
        }
    }
    
    callback(true);  // Accept the edit
}

Client Script Best Practices

  • Performance: Keep scripts lightweight and efficient
  • Avoid: Synchronous GlideAjax calls, heavy loops
  • Always Check: isLoading and isTemplate parameters in onChange
  • Return Values: onSubmit must return true/false
  • Messages: Use g_form methods for user feedback
  • Scope: Global client scripts run on all tables, use sparingly

CSA & CAD Exam Questions (15 Questions)

Which client script type executes when a form is loaded? Q1
CSA
  • A) onChange
  • B) onLoad
  • C) onSubmit
  • D) onStart
Show Answer
Correct Answer: B
onLoad client scripts execute when a form loads. Use them to set default values, make fields mandatory, or configure the form based on initial conditions.
How do you prevent a form from submitting in an onSubmit client script? Q2
CSA
  • A) return null
  • B) return false
  • C) g_form.stopSubmit()
  • D) g_form.preventDefault()
Show Answer
Correct Answer: B
Return false from an onSubmit client script to prevent form submission. Return true to allow submission.
What parameter indicates if an onChange script is running during form load? Q3
CSA
  • A) isLoading
  • B) onLoad
  • C) isInitial
  • D) formLoad
Show Answer
Correct Answer: A
The isLoading parameter is true when the onChange script runs during form load. Always check: if (isLoading || isTemplate) return;
Which client script type can prevent inline list edits? Q4
CAD
  • A) onLoad
  • B) onChange
  • C) onCellEdit
  • D) onListEdit
Show Answer
Correct Answer: C
onCellEdit client scripts validate inline list edits. Call callback(false) to reject the edit or callback(true) to accept it.
Can client scripts directly query the database? Q5
CSA
  • A) Yes, using GlideRecord
  • B) Yes, using GlideQuery
  • C) No, use GlideAjax instead
  • D) Yes, using g_form.query()
Show Answer
Correct Answer: C
Client scripts run in the browser and cannot directly access the database. Use GlideAjax to call server-side Script Includes for database operations.
What is the purpose of the 'control' parameter in onChange scripts? Q6
CAD
  • A) It contains the new value
  • B) It references the DOM element of the field
  • C) It contains the old value
  • D) It controls form submission
Show Answer
Correct Answer: B
The control parameter references the DOM element of the field that changed. Use control.id to get the field name.
How do you display an error message to the user in a client script? Q7
CSA
  • A) alert('message')
  • B) g_form.addErrorMessage('message')
  • C) console.error('message')
  • D) gs.error('message')
Show Answer
Correct Answer: B
Use g_form.addErrorMessage() to display error messages in client scripts. Also available: addInfoMessage() and clearMessages().
Which method makes a field mandatory in a client script? Q8
CSA
  • A) g_form.setRequired('field', true)
  • B) g_form.setMandatory('field', true)
  • C) g_form.mandatory('field')
  • D) g_form.required('field', true)
Show Answer
Correct Answer: B
Use g_form.setMandatory('field', true) to make a field mandatory, and g_form.setMandatory('field', false) to remove the requirement.
What happens if an onSubmit script doesn't return a value? Q9
CAD
  • A) Form submission is prevented
  • B) Form submission proceeds (treated as true)
  • C) An error is thrown
  • D) User is prompted to confirm
Show Answer
Correct Answer: B
If an onSubmit script doesn't explicitly return false, the submission proceeds. Always explicitly return true or false.
Where do client scripts execute? Q10
CSA
  • A) On the ServiceNow server
  • B) In the user's web browser
  • C) In the database
  • D) On both server and client
Show Answer
Correct Answer: B
Client scripts execute in the user's web browser, not on the server. This is why they cannot directly access the database.
Which parameter in onChange contains the previous field value? Q11
CSA
  • A) control
  • B) oldValue
  • C) newValue
  • D) previousValue
Show Answer
Correct Answer: B
onChange signature: function onChange(control, oldValue, newValue, isLoading, isTemplate). oldValue contains the previous value.
Can global client scripts impact system performance? Q12
CAD
  • A) No, they're optimized by the system
  • B) Yes, they run on every form load across all tables
  • C) Only if they query the database
  • D) Performance impact is negligible
Show Answer
Correct Answer: B
Global client scripts run on every form across all tables, which can significantly impact performance. Use table-specific scripts when possible.
How do you hide a field in a client script? Q13
CSA
  • A) g_form.hideField('field')
  • B) g_form.setDisplay('field', false)
  • C) g_form.setVisible('field', false)
  • D) g_form.hide('field')
Show Answer
Correct Answer: B
Use g_form.setDisplay('field', false) to hide a field and g_form.setDisplay('field', true) to show it.
What is the isTemplate parameter used for in onChange? Q14
CAD
  • A) Indicates if the form is a UI template
  • B) Indicates if values are being set from a template
  • C) Indicates if the script is a template script
  • D) Not used in ServiceNow
Show Answer
Correct Answer: B
isTemplate is true when field values are being set from a template. Check this to avoid running logic during template application: if (isLoading || isTemplate) return;
Can you have multiple onSubmit client scripts on the same table? Q15
CAD
  • A) No, only one is allowed
  • B) Yes, all active scripts execute in order
  • C) Yes, but only the first one runs
  • D) Yes, but they run randomly
Show Answer
Correct Answer: B
Multiple onSubmit scripts can exist on the same table. They execute in order (by Order field). If ANY returns false, submission is prevented.

Key Takeaways

  • Four types: onLoad, onChange, onSubmit, onCellEdit
  • Run in browser, not server
  • Check isLoading & isTemplate
  • Return false to prevent submission
  • Use g_form API for form manipulation
  • Avoid global client scripts