0

A client script in ServiceNow is a type of script that runs on the client side, meaning it is executed in the web browser of the person accessing the ServiceNow platform. Client scripts can be used to validate user input, set default values for fields, or perform other tasks to enhance the user experience on the ServiceNow platform.
What are different types of Client script in ServiceNow ?
There are several different types of client scripts in ServiceNow, including:

  1. OnLoad scripts: These scripts are executed when a form or page is loaded in the web browser. OnLoad scripts can be used to set default values for fields, modify the behavior of UI elements, or perform other tasks to prepare the form or page for user interaction.
  2. OnChange scripts: These scripts are executed when the value of a form field is changed by the user. OnChange scripts can be used to perform validation on user input, update other fields based on the new value, or take other actions in response to the change.
  3. OnSubmit scripts: These scripts are executed when a user submits a form by clicking the Save button or performing another action that triggers a submission. OnSubmit scripts can be used to perform final validation on the form data, gather additional information, or perform other tasks before the form data is saved to the ServiceNow database.
  4. OnCellEdit scripts: These scripts are executed when a user edits the value of a cell in a list or table. OnCellEdit scripts can be used to validate the new value, update other cells based on the new value, or take other actions in response to the edit.

Example of onLoad Client script
1. OnLoad Client Script :

function onLoad() {
  
  g_form.setValue('short_description', 'Workingcode.in');
      if (g_form.getValue('caller_id') == '') {
    g_form.setDisplay('email', false);
  }
}

2. onChange Client Script :

  • An example of an onChange client script in ServiceNow might look like this
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
   // If the user changes the value of the "Category" field,
  // set the default value for the "Subcategory" field
  if (g_form.getValue('name') == 'workingcode') {
    if (newValue == 'tablet') {
      g_form.setValue('subcategory', 'tablet');
    } else if (newValue == 'Software') {
      g_form.setValue('subcategory', 'Operating System');
    }
  }
}

3. onSubmit Client script :

  • An example of an onSubmit client script in ServiceNow might look like this
    function onSubmit() {
       if (g_form.getValue('name') == '') {
        g_form.showFieldMsg('name', 'Name is required', 'error');
        return false;
      }
      g_form.setValue('caller_id', g_user.userID);
      g_form.setValue('opened_at', g_form.getCurrentDateTime());
       return true;
    }

    4. OnCellEdit Client Script 

    function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
     var saveAndClose = true;
     var isAdmin = g_user.hasRole('admin');
         if ((!isAdmin && newValue == 2) || (!isAdmin && newValue == 3)){
             alert('Not allowed to set this state');
             saveAndClose = false;
        }
       callback(saveAndClose); 
    }
What is ServiceNow Client Script ?
Working Code Edited question September 27, 2023