duplicate checking for multiple fields using business rule

Kajal16
Kilo Contributor

Here i want to avoid duplicate values  for six fields using business rule and  applied this code .Here for one field it is working correctly but for two or more than that it is not working. It simply takes the values in table and also showing" invalid insert message" it shows this message and also gets inserted in the table 

find_real_file.png

Here is the code that i applied 

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

// Add your code here
var grAst = new GlideRecord('tablename');


grAst.addQuery('name', current.name);
grAst.addQuery('roll', current.roll);
grAst.addQuery('address', current.address);
grAst.addQuery('city', current.city);
grAst.addQuery('school', current.school);
grAst.addQuery('pincode', current.pincode);
grAst.setLimit(1);

grAst.query();

if (grAst.hasNext() && grAst.sys_id != current.sys_id)
{
gs.addErrorMessage("Serial number already exists");

current.setAbortAction(true);


}

 

5 REPLIES 5

Chuck Tomasi
Tera Patron

Are you looking to ensure that the record has unique values for all six fields or that all six fields have unique values?

Example 1:

RecA: name=chuck.tomasi, id=12345, dev=ffda

RecB: name=tom.smith, id=12345, dev=3dee

In your requirement, am I allowed to save recB if recA already exists or are these mutually exclusive? 

Your script suggests that I can save B. If your requirement is that it should not be saved because the IDs are the same, then I recommend setting the dictionary field "unique=true" on each of the six fields you want to ensure uniqueness.