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()viaGlideAjax, 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, extendingAbstractAjaxProcessor, does the actualsys_user_grouplookup 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.
| |