ServiceNow Client Script OnSubmit Real-Time Scenarios and Use Cases

As we have discussed earlier, ServiceNow offers a cloud-based suite of tools to automate and optimize business processes. Using client scripts to improve user interactions and data processing is one of its strong points. The OnSubmit client script is beneficial because it enables developers to run custom code in response to a form submission.

This article, let examines the OnSubmit client script and provides use cases and real-world scenarios to illustrate its usefulness.

OnSubmit client script

In ServiceNow, client scripts communicate with the ServiceNow instance by running in the user's browser. They can be applied to many different tasks, such as field manipulation, form validation, and user interface improvements. When a user tries to submit a form, the OnSubmit client script is activated, giving you a chance to do last-minute checks or enforce policies before data is saved.

Key features of the onSubmit client script

  • Runs code capable of validating a field or the entire form.
  • May also be used to stop a form from being submitted if specific requirements aren't fulfilled.
  • Creating an OnSubmit Client Script, In ServiceNow, follow these steps to create an OnSubmit client script.
  • Go to Client Scripts > System Definition.
  • To start writing a new script, click New.
  • Select "OnSubmit" for the Type.
  • Name the Table to which the script will be applied.
  • To specify the behavior when the form is submitted, you can start writing the script.

Real-Time Scenarios and Use Cases

Step 1. Mandatory Field Validation Scenario Before permitting the submission of a form, make sure that a few fields are completed.

Use Case. Prior to the form being submitted, the Category and Impact fields on the Incident form need to be completed.

function onSubmit() {
    var category = g_form.getValue('category');
    var impact = g_form.getValue('impact');  
    if (!category || !impact) {
        g_form.addErrorMessage('Category and Impact are mandatory fields.');
        return false;
    }
    return true;
}

Step 2. In the event that the Due Date falls before the Start Date, the Date Validation Scenario will stop submission.

Use Case. To maintain logical consistency, make sure users on a task form do not enter a due date that is earlier than the start date.

function onSubmit() {
    var startDate = g_form.getValue('start_date');
    var dueDate = g_form.getValue('due_date');   
    if (dueDate < startDate) {
        g_form.addErrorMessage('Due Date cannot be earlier than Start Date.');
        return false;
    }
    return true;
}

Step 3. Avoid the Duplicate Records Scenario by making sure there are no duplicate records before submitting a form.

Use Case. If an incident already exists with the same Short Description, do not allow submission of the incident form.

function onSubmit() {
    var shortDescription = g_form.getValue('short_description');
    var gr = new GlideRecord('incident');
    gr.addQuery('short_description', shortDescription);
    gr.query();
    if (gr.next()) {
        g_form.addErrorMessage('An incident with this short description already exists.');
        return false;
    }
    return true;
}

Step 4. Enforcement of Special Business Rules.

Situation. Prior to permitting form submission, enforce intricate business rules.

Use Case. Verify that an Approver is chosen on the Change Request form if the Change Type is Emergency.

function onSubmit() {
    var changeType = g_form.getValue('change_type');
    var approver = g_form.getValue('approver');
    if (changeType == 'Emergency' && !approver) {
        g_form.addErrorMessage('An approver must be selected for Emergency changes.');
        return false;
    }
    return true;
}

Step 5. Field Consistency Check Scenario: Verify, prior to submission, that specific fields are consistent with one another.

Use Case. Verify that the Hardware Details field on the Service Request form is filled in if the Request Type is New Hardware.

function onSubmit() {
    var requestType = g_form.getValue('request_type');
    var hardwareDetails = g_form.getValue('hardware_details');   
    if (requestType == 'New Hardware' && !hardwareDetails) {
        g_form.addErrorMessage('Hardware Details must be provided for New Hardware requests.');
        return false;
    }
    return true;
}

Guidelines for Using Client Scripts on OnSubmit

  • To preserve readability and performance, OnSubmit scripts should not contain excessively complicated logic.
  • Include comments in your code to describe the function and goal of each section.
  • Before deploying OnSubmit scripts to production, they should always be tested in a development or test environment.
  • Give users unambiguous error messages that instruct them on what needs to be fixed.

Before a form is submitted, the ServiceNow OnSubmit client script can be used as a strong tool to enforce business rules and guarantee data integrity. You can enforce intricate business logic, prevent duplicate records, and build strong validation processes by utilizing this feature. The OnSubmit script provides a flexible solution for various scenarios, such as verifying field dependencies, verifying mandatory fields, or examining data consistency. To achieve optimal performance and reliability, follow best practices and make sure you test everything thoroughly, just like you would with any customization.

Until the next article, happy coding and all the very best.


Similar Articles