avoid duplicate entry in a table by business rule

Raju Singh1
Tera Expert

I am trying to avoid a duplicate entry in a table using a business rule but it is not working.

Instead of firing for duplicate entires.. Its fires for everything. even not allowing me to add the fresh entry itself.

I am trying to eliminate a duplicate entry for a serial no. in a computer table.

Here is my code:

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

// Add your code here

var gr = new GlideRecord('cmdb_ci_computer');

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

gr.query();

if(gr.next()){

gs.addInfoMessage('There is duplicate value for this serial number in the table');

current.setAbortAction(true);

}

})(current, previous);

1 ACCEPTED SOLUTION

snehabinani26
Tera Guru

Hi Raju,



You can have a before BR with the below script.



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


  var grAst = new GlideRecord('cmdb_ci_computer');


  grAst.addQuery('serial_number', current.serial_number);


  grAst.query();


  if (grAst.next() && grAst.sys_id != current.sys_id) {


      gs.addErrorMessage("Serial number already exists");


      current.setAbortAction(true);


  }


})(current, previous);



Hope this helps you.


View solution in original post

24 REPLIES 24

marcguy
ServiceNow Employee
ServiceNow Employee

add this in



gr.addQuery('sys_id','!=',current.sys_id);



this stops the record from seeing itself...



make sure it's a before BR


Thanks mguy for replying, but its not allowing me to enter the fresh record itself... Is there something wrong in my code?


gopi2203
Tera Expert

HI Raju,



the business rule should be before insert business rule


Yes, I have put the business rule before insert. Please see the screenshot