Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to validate duplicate check

Devi-01
Kilo Explorer

Hi everyone,

 

I’m working on a Service Catalog item (e.g., “Software License Request”) where the user enters a license number.

 

My requirement is to:

 

* Check in real-time if the entered license number already exists in the system (e.g., in sc_req_item or a custom table)

* If a duplicate is found, show an error message immediately

* Optionally provide a link to the existing request

 

I’m planning to use:

 

* onChange Catalog Client Script

* GlideAjax

* Script Include

 

My questions:

 

1. Is this the correct/best approach for real-time validation?

2. Are there any best practices or alternative methods (like Data Policy / Business Rule)?

3. How can I efficiently return the existing record link to display in the message?

 

Any sample implementation or guidance would be really helpful.

 

Thanks in advance!

1 REPLY 1

Naveen20
ServiceNow Employee

You can try as below

var LicenseValidator = Class.create();
LicenseValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkDuplicate: function() {
var lic = (this.getParameter('sysparm_license') || '').trim();
var result = { exists: false };
if (!lic) return JSON.stringify(result);

var gr = new GlideRecord('sc_req_item');
gr.addQuery('variables.license_number', lic);
gr.addActiveQuery();
gr.setLimit(1);
gr.query();

if (gr.next()) {
result.exists = true;
result.number = gr.getValue('number');
result.url = '/nav_to.do?uri=sc_req_item.do?sys_id=' + gr.getUniqueValue();
}
return JSON.stringify(result);
},

type: 'LicenseValidator'
});

onChange Catalog Client Script on license_number:

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue || newValue === oldValue) return;

g_form.hideFieldMsg('license_number', true);

var ga = new GlideAjax('LicenseValidator');
ga.addParam('sysparm_name', 'checkDuplicate');
ga.addParam('sysparm_license', newValue);
ga.getXMLAnswer(function(response) {
var r = JSON.parse(response);
if (r.exists) {
var msg = 'License already exists on <a href="' + r.url +
'" target="_blank">' + r.number + '</a>.';
g_form.showFieldMsg('license_number', msg, 'error', true); // 4th arg = isHTML
}
});
}


Best practices
- Add a before-insert BR on sc_req_item with the same check + current.setAbortAction(true) — client scripts can be bypassed via API.
- Data Policy / UI Policy don't fit here.