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

Abhinay Erra
Giga Sage

Write a before insert business rule and put this script in there



var gr= new GlideRecord('core_company');


gr.addQuery('u_vndr_id',current.u_vndr_id);


gr.query();


if(gr.hasNext()){


current.setAbortAction(true);


}


Nice! . Could we also add in a message stating "Vendor ID Already exists" when a match is found?


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


}


Thanks Abhinay!   This is exactly what I was asking for.  


find_real_file.png




@devi_putra , Thank you for your input as well.   While your answer is also correct, I was looking for the script including the pop up message.