Require unique value on a field

carlh
Kilo Guru

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

1 ACCEPTED SOLUTION

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);


}


View solution in original post

11 REPLIES 11

Aaron Schmid
Tera Contributor

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' });


  }



}


Damhoej
Giga Guru

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.