How Many types of client scripts in ServiceNow

UdayG0047661101
Kilo Explorer

Hi Family,

 

Can anyone tell me How Many types of client scripts in ServiceNow?

 

Many Thanks,

Abel Tuter

6 REPLIES 6

Hi @UdayG0047661101 

 

Here All 4 client scripts with sample code as a example.

 

  1. Onload ()

Common Use Cases: Setting default field values, hiding or showing fields on load, or making fields read-only based on roles.

Code Example:

function onLoad() {

    if (g_form.isNewRecord()) {

                g_form.setValue('impact', '3');

               g_form.addInfoMessage('Welcome! Please fill out all necessary field details.');

    }

}

 

  1. onChange()

Common Use Cases: Making a field mandatory based on another choice, showing a section, or validating input format.

Code Example:

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

    if (isLoading || newValue === '') {

        return;

    }

    if (newValue == 'hardware') {

        g_form.setMandatory('u_serial_number', true);

        g_form.showFieldMsg('u_serial_number', 'Serial number is strictly required for hardware items.', 'info');

    } else {

        g_form.setMandatory('u_serial_number', false);

    }

}

 

3.onSubmit()

  • Common Use Cases: Final multi-field form validations or preventing submissions when formatting or security constraints are broken.

Code Example:

function onSubmit() {

    var shortDesc = g_form.getValue('short_description');

        if (shortDesc.length < 10) {

        g_form.addErrorMessage('Submission aborted: The short description must be at least 10 characters long.');       

        return false;

    }   

    return true;

}

  1. onCellEdit()
  • Common Use Cases: Restricting mass inline updates, validating list changes, or locking down specific columns based on user status.

Code Example:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

    var saveAndClose = true;

    if (newValue == '7') {

         alert('Security Notice: Records cannot be Closed directly via the list view view. Please open the form.');

        saveAndClose = false;

    }

    callback(saveAndClose);

}

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

nitin gupta1
Tera Contributor

ServiceNow Client Script Types (4 Types)

1. onLoad()
- Executes when the form loads
- Used to set default values, hide/show fields, initialize form

Example:
function onLoad() {
g_form.setValue('priority', '2');
}


2. onChange()
- Executes when a field value changes
- Used to validate input or update other fields dynamically

Example:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '1') {
g_form.setValue('state', 'In Progress');
}
}


3. onSubmit()
- Executes when the form is submitted
- Used for validation before submission

Example:
function onSubmit() {
if (!g_form.getValue('short_description')) {
alert('Short Description is required');
return false;
}
}


4. onCellEdit()
- Executes when a list cell is edited (list view, not form)
- Used to validate or control inline list editing

Example:
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
if (newValue == '') {
alert('Value cannot be empty');
return false;
}
}