- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2016 09:02 AM
Good morning.
How can I set up a field to query the table for unique values before allowing me to save a record to the table?
I wasn't sure how to word it but here's what I am doing. We are creating new vendors and need to check and make sure the "VendorID" field is not already in the table.
I'm hoping I can do this as a client script so it can be caught while on the form and fixed.
Could someone provide a script?
Table "core_company"
Field Name: "u_vndr_id" (string)
I have several field where I could use this as well so hoping it's as easy as changing the table and fields.
Thanks!!! Carl
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2016 09:23 AM
Sure, here you go
var gr= new GlideRecord('core_company');
gr.addQuery('u_vndr_id',current.u_vndr_id);
gr.query();
if(gr.hasNext()){
gs.addErrorMessage('Vendor ID Already exists');
current.setAbortAction(true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2017 11:36 AM
Just to add to this...
I wanted an alert onChange version in a client script and this worked for me...
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var gr = new GlideRecord('table_name');
gr.addQuery('field_name', newValue);
gr.query();
if(gr.hasNext()){
g_form.setValue('field_name', ' ');
alertNow ({header: 'custom text', title:'custom text', message: 'custom text', image:'url', imageWidth:'250', imageHeight:'250' });
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 02:16 AM
https://docs.servicenow.com/bundle/jakarta-platform-administration/page/administer/field-administration/task/t_RequiringUniqueValuesForAField.html
It is for the Jakarta version, but I will imaging that the solution is the same for other newer versions.