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

here is the script


Thanks Mate!

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.


Still it doesn't allow me to enter any value and gives a screen message. Its not checking the condition


Code provided you is working for us for alm_asset table in Production. So it shouldn't cause any issue.