how to set the field value unique using script ?

sk227
Kilo Explorer

Hi,

i have two fields X and Y,

if my X- field has a value "123" and Y - field has a value as "456"

then my Y field should not take any other   value as "456" (for my X - field "123" ). it should throw an error as "record exist already".

Please help me out with using script.

6 REPLIES 6

Manoj Kumar16
Giga Guru

The values of X field and Y fields get stored in table when they submitted right ?


Then you can create an onchange client script and add error message if you check that table that X or Y field's value already exists.


tolga1
ServiceNow Employee
ServiceNow Employee

Business Rule for insert:



(function executeRule(current, previous /*null when async*/) {


// Add your code here


if (current.y &&


(current.operation() == "insert" ||


(current.operation() == "update" && current.y.changes()))) {


var gr = new GlideRecord("table_where_x_and_y_are");


gr.addQuery("y", current.y);


gr.query();


if (gr.getRowCount() > 0) {


current.setAbortAction(true);


gs.addErrorMessage(gs.getMessage("Not allowed the duplicate y=" + current.y));


}


}


})(current, previous);



--> replace y with your field value and you're done.


sk227
Kilo Explorer

Thanks for the response


i got the solution by writing a business rule.


tolga1
ServiceNow Employee
ServiceNow Employee

hi sarath. please mark as helpful and correct, if my answer helped. thanks