I am working on a validation requirement for the Change Request table and need help with a character

PratikshaAbhang
Tera Contributor

Requirement:

  1. Please Note: This validation is not applicable for Data Patch tickets where the subtype is 400, 500, or 600. but this subtype is populated after submitting change request

  2. Character Limits:

    • Minimum of 100 characters for the following sections:

      • Description
      • Justification
      • Risk and Impact Analysis
    • Minimum of 15 characters for the following sections:

      • Implementation Plan
      • Backout Plan
1 REPLY 1

Moin Kazi
Kilo Sage
Kilo Sage

Hi @PratikshaAbhang ,

 

To implement this character validation in ServiceNow, you can use a combination of Client Scripts and Business Rules. Since the subtype (400, 500, 600) is populated after submission, the validation should be checked at both the client and server sides.

 

Here’s a step-by-step solution:

Step 1: Client Script for Character Validation on Form Submission

Create a client script to validate the character limits on form submission.

  1. Navigate to System Definition > Client Scripts.

  2. Click New to create a new Client Script.

  3. Configure the script as follows:

    • Name: Character Limit Validation
    • Table: Change Request (or your relevant table)
    • Type: onSubmit
    • Script:
       

 

function onSubmit() {
    //Use !g_form.getValue('u_subtype') to ensure it runs only if the subtype is undefined at this stage.
    // Check for character limits
   if(!g_form.getValue('u_subtype')){
    if (g_form.getValue('description').length < 100) {
        alert('Description must be at least 100 characters.');
        return false;
    }
    if (g_form.getValue('justification').length < 100) {
        alert('Justification must be at least 100 characters.');
        return false;
    }
    if (g_form.getValue('risk_and_impact_analysis').length < 100) {
        alert('Risk and Impact Analysis must be at least 100 characters.');
        return false;
    }
    if (g_form.getValue('implementation_plan').length < 15) {
        alert('Implementation Plan must be at least 15 characters.');
        return false;
    }
    if (g_form.getValue('backout_plan').length < 15) {
        alert('Backout Plan must be at least 15 characters.');
        return false;
    }}
    return true;
}

 

This will check the character limits before submission for users on the client side.

 

Step 2: Business Rule to Enforce Validation After Submission

Since the subtype is only set after submission, a Business Rule will validate the fields server-side for any tickets that don’t meet the requirements.

  1. Navigate to System Definition > Business Rules.

  2. Click New to create a Business Rule.

  3. Configure the Business Rule as follows:

    • Name: Post-Submission Character Validation
    • Table: Change Request (or relevant table)
    • When to Run: Before Insert/Update
    • Condition: current.u_subtype != 400 && current.u_subtype != 500 && current.u_subtype != 600
    • Script:
       

 

(function executeRule(current, previous /*null when async*/) {
    var errorMessage = '';

    if (current.description.length < 100) {
        errorMessage += 'Description must be at least 100 characters.\n';
    }
    if (current.justification.length < 100) {
        errorMessage += 'Justification must be at least 100 characters.\n';
    }
    if (current.risk_and_impact_analysis.length < 100) {
        errorMessage += 'Risk and Impact Analysis must be at least 100 characters.\n';
    }
    if (current.implementation_plan.length < 15) {
        errorMessage += 'Implementation Plan must be at least 15 characters.\n';
    }
    if (current.backout_plan.length < 15) {
        errorMessage += 'Backout Plan must be at least 15 characters.\n';
    }

    if (errorMessage) {
        gs.addErrorMessage(errorMessage);
        current.setAbortAction(true);
    }
})(current, previous);

 

This business rule checks the character limit after the subtype is set on submission, blocking the update if the conditions aren’t met.

Using these client and server-side validations ensures that character limits are enforced, even if users bypass the client script.

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~