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

Hey, I want to do it without Serial Numbers. If some one enters a CI with the same name, it should not allow the user to submit the CI. Any idea how would that work.

Any help would be appreciated.

this code is working for one field .I want to use for first it will check id and If the id matches then it will check name, address and phone number. 

Hi @snehabinani26 ,

Thank you it's really helpful.

 
 
snehabinani26
ServiceNow Principal Consultant
Correct Answers 100
Helpful Answers 200
2018 Now Community MVP
11,850 points - Community Level 
 
 
snehabinani26
ServiceNow Principal Consultant
Correct Answers 100
Helpful Answers 200
2018 Now Community MVP
11,850 points - Community Level 6

Khanna Ji
Tera Guru

There is no issue in your script, its perfectly fine. There can be some records having empty serial numbers and I think you are trying to enter a record with empty serial number. Please try below code once;



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



if(current.serial_number == '' || current.serial_number == null)


{


gs.addErrorMessage('Serial Number cannot be empty');


current.setAbortAction(true);


}



var gr = new GlideRecord('cmdb_ci_computer');


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


gr.addNotNullQuery('serial_number');


gr.query();


if(gr.next()){


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


current.setAbortAction(true);


}




})(current, previous);


Hi Swathi,



Thank you for the script provided. Its working but it is not saving the date for the other fields when I create a new record. But it allows to update a record. for example please see I have created a record. which will only save the serial no. but not the name of the computer.



But when I open the record, then it allows me to update the name field which was blank initially. Please find 4 screen shots which I have sent.



Thank you so much for your help.. I appreciate this.



Regards,


Raju Singh