Business Rule to avoid Duplicate entries
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 02:31 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2017 02:37 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2017 09:22 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 01:54 AM
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
