OnLoad Client Script In ServiceNow

 OnLoad Client Script:

  • Runs When Form Opens — In ServiceNow, this script automatically runs in the background as soon as a form opens, before the user can click or type anything on the form.
  • Controls How Form Looks — It is mainly used to change the form appearance like hiding fields, making fields mandatory, or pre-filling values so that form is already set up when user sees it


Basic structure:

ServiceNow Code
function onLoad() {
    // Your code runs here when the form opens
}

onLoad Client Script Examples:

1. Hide a field on form load.

ServiceNow Code
function onLoad() {

    // Hide Subcategory field when form opens
    g_form.setVisible('subcategory', false);
}

2. Show a Welcome Message for New Records

ServiceNow Code
function onLoad() {

    // Check if this is a new record
    if (g_form.isNewRecord()) {
        g_form.addInfoMessage('Please fill all mandatory fields before submitting.');
    }
}

3.Make a Field Read-Only Based on User Role.

ServiceNow Code
function onLoad() {

    // Check if user has 'itil_admin' role
    var isAdmin = g_user.hasRole('itil_admin');

    if (!isAdmin) {
        // Lock the Priority field for non-admin users
        g_form.setReadOnly('priority', true);
    }
}

4. Set Default Value on Form Load.

ServiceNow Code
function onLoad() {

    // Set default value only for new records
    if (g_form.isNewRecord()) {

        // Set default contact type to 'Phone'
        g_form.setValue('contact_type', 'phone');

        // Set default impact to Medium (2)
        g_form.setValue('impact', '2');
    }
}

5.Show Warning Message for High Priority Records.

ServiceNow Code
function onLoad() {

    // Get the priority value of the current record
    var priority = g_form.getValue('priority');

    // Show warning if Priority is Critical (1)
    if (priority === '1') {
        g_form.addErrorMessage('CRITICAL Priority — This incident needs immediate attention!');
    }
}

6. Display Current User Info on Form Load.

ServiceNow Code
function onLoad() {

    // Get current logged-in user details
    var userID   = g_user.userID;
    var userName = g_user.userName;
    var fullName = g_user.firstName + ' ' + g_user.lastName;

    // Auto-fill caller on new records only
    if (g_form.isNewRecord()) {
        g_form.setValue('caller_id', userID);

        g_form.addInfoMessage(
            'Logged in as: ' + fullName
        );
    }
}

7. Make Fields Mandatory Based on Record State.

ServiceNow Code
function onLoad() {

    // Get current state of the record
    var state = g_form.getValue('state');

    // State 2 = In Progress, State 6 = Resolved
    if (state === '2') {
        // In Progress — Assignment Group is required
        g_form.setMandatory('assignment_group', true);
        g_form.setMandatory('assigned_to', true);

    } else if (state === '6') {
        // Resolved — Close notes is required
        g_form.setMandatory('close_notes', true);
        g_form.setMandatory('close_code', true);

        // Also lock key fields — record is resolved
        g_form.setReadOnly('short_description', true);
    }
}

Post a Comment

0Comments
Post a Comment (0)