Business Rule to avoid Duplicate entries

Sachin Shivaku1
Giga Guru

Hi All,

Below is the BR i have written to avoid duplicate entries in a table(related-list).


I am going wrong in the code can anyone guide me on this

Before - Insert/Update

CODE:

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

try{

// Add your code here

var tilNum = current.u_til_num;

  var mc = new GlideRecord('u_top_issues_portal'); //table name

  mc.addQuery('sys_id',current.u_main_case);

  mc.query();

  while(mc.next()){

var gr_til = new GlideRecord('u_til_relationship'); //related-list table name

gr_til.addQuery('u_til_num', tilNum);

//gr.addNotNullQuery('u_til_num');

gr_til.query();

if(gr_til.next()){

gs.addErrorMessage('The TIL Number selected is already present in the list');

current.setAbortAction(true);

}

}

}

catch(e){

gs.addErrorMessage('Exception Occurred'+e);

}

})(current, previous);

Thanks,
Sachin

3 REPLIES 3

Karthik Reddy T
Kilo Sage

Hello Sachin,



Refer the below similar threads may helpful to you.



Script to stop duplicates applying on a related table



Business Rules Best Practices - ServiceNow Wiki


Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

Hello Sachin,



May i know the status of this thread?


Let me know if that answered your question. If so, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.


How To Mark Answers Correct From Community Inbox


Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

Hello Karthik,



Sorry for the delay.



I made use of the below code to remove the duplicate records.



CODE:



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


try{




var gr_til = new GlideRecord('u_til_relationship'); //related-list table name


gr_til.addQuery('u_main_case',current.u_main_case);


gr_til.addQuery('u_til_num', current.u_til_num);


gr_til.query();



if(gr_til.next()){


gs.addErrorMessage('The TIL Number selected is already present in the list');


current.setAbortAction(true);


}


}


catch(e){


gs.addErrorMessage('Exception Occurred'+e);


}



})(current, previous);



Regards,


Sachin