Skip to content
Field Uniqueness Validation via GlideAjax

Field Uniqueness Validation via GlideAjax

Overview

This pattern validates a user-entered field value against a table server-side, before the form is allowed to submit, using a paired onChange + onSubmit Catalog Client Script and a shared Script Include. The example here validates a new distribution group’s email address against sys_user_group (to prevent creating a group with an email that’s already in use), via a CheckGroupEmailExists Script Include:

  • onChange — fires as soon as the user finishes typing an email address. It calls CheckGroupEmailExists.checkEmail() via GlideAjax, and if a match is found, clears the field and shows an inline field error immediately, so the user gets fast feedback while still on the field.
  • onSubmit — performs the same check again at submit time as a safety net (covering cases like autofill, paste, or a value that was valid when last checked but became a duplicate since), and blocks submission if a duplicate is found.
  • Script Include — CheckGroupEmailExists, extending AbstractAjaxProcessor, does the actual sys_user_group lookup for an active group with the given email and returns 'true'/'false' as a string.

Problem Statement

Some catalog variables need to be unique or otherwise validated against a table that isn’t exposed as a simple reference field — for instance, a free-text “email address” variable that must not collide with an existing group’s email. Client-side JavaScript alone can’t query server tables, so the check has to go through GlideAjax to a Script Include. Checking only onChange isn’t fully reliable (a user could bypass the change event or the value could go stale), so pairing it with an onSubmit check guarantees the value is verified again immediately before the record is saved.

Validate Distribution Email - onChange.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * Script Name: Validate Distribution Email - onChange
 * Description:
 *     Catalog client onChange script for ServiceNow that checks if the entered distribution email address already exists as a group.
 *     When the email_address field value changes, it calls the 'CheckGroupEmailExists' Script Include via GlideAjax.
 *     If the email exists, an error message is shown on the field; otherwise, any existing error message is hidden.
 *
 * Usage:
 *     Attach as an onChange client script to the 'email_address' field in a ServiceNow catalog item.
 *
 * Author: Konner Lester
 * Date Created: 8/1/2025
 * Last Modified: 8/4/2025
 */
function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
    return;
 }

 var ga = new GlideAjax('CheckGroupEmailExists');
 ga.addParam('sysparm_name', 'checkEmail');
 ga.addParam('sysparm_email', newValue);
 ga.getXMLAnswer(function(response) {
    if (response === 'true') {
      g_form.clearValue('email_address');
      g_form.showFieldMsg('email_address', 'This email is already in use by another group.', 'error');
    } else {
      g_form.hideFieldMsg('email_address', true);
    }
 });
}
Last updated on