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

Raju Sardar wrote:



Yes you are right Dave, but as there are already duplicate entries.. I was just afraid that I may loose some data if I make it unique value now. What's the best practice?


I believe uniqueness is checked upon insert/update, but not on existing data, so you should be able to enable it without it harming current records.



However, I'd recommend you clone your instance to a testbed and try it out first. It's always the way with major changes of this kind: don't risk production systems if you're unsure.


Hi. I am a beginner in service now. Can you please tell me how can I make a field unique in order for me to prevent duplicates?

Hi Pearl,

 

There is no OOB logic for this.

You have to write the above mentioned BR(Business Rule) in order to make any field unique and avoid duplicate enteries.

 

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

 

  var grAst = new GlideRecord('cmdb_ci_computer');

 

  grAst.addQuery('serial_number', current.serial_number); // you can put your unique field here to make it unique and it doesnt allow to put duplicate entry and will stop user doing that

 

  grAst.query();

 

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

 

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

 

      current.setAbortAction(true);

 

  }

 

})(current, previous);

 

 

Regards,Shamma Negi

Shamma Negi
Kilo Sage
Kilo Sage

MAke sure that you are running on before insert only.


ake sure you add few gslog statement in it.



Regards,


Shamma


Regards,Shamma Negi

Raju Singh1
Tera Expert

All your suggetions were helpful.. A bit of modification made it work. The script was correct which you guys gave. Thank you for your help