
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2019 03:48 AM
Hi All,
We recently have an issue with Duplicate Change Task/ Change Numbers being created in the system. We had raised a HI ticket with Service Now for the same and they suggested for creating a business rule which would check for Duplicates. Can anyone please share the same if already implemented ?
Awaiting response.
Thanks in Advance.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2019 04:13 AM
HI,
What i assume is that, whenever you are trying to create a new record duplicate number is getting inserted and hence you are getting issue. SO you want a script which should not allow a insertion of change task/request if the number already exists. IF so then use below script.
//For CHG Request (Before Insert BR)
var gr = new GlideRecord('change_request');
gr.addQuery('number',current.number);
gr.query();
if(gr.next()){
current.setAbortAction(true);
gs.addInfoMessage('Number is Duplicate');
}
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2019 03:53 AM
Hi Arindam,
Following script should help you detect the duplicates
create a fix script for this and put the duplicates in log
script below for change_task
gs.info('Duplicates are: ' + getDuplicates('change_task','number'));
function getDuplicates(tablename,val) {
try{
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT',val);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords;
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
script below for change_request
gs.info('Duplicates are: ' + getDuplicates('change_request','number'));
function getDuplicates(tablename,val) {
try{
var dupRecords = [];
var gaDupCheck = new GlideAggregate(tablename);
gaDupCheck.addAggregate('COUNT',val);
gaDupCheck.addNotNullQuery(val);
gaDupCheck.groupBy(val);
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
dupRecords.push(gaDupCheck[val].toString());
}
return dupRecords;
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2019 05:27 AM
Thank you Ankur for your reply.
I could find the duplicate Change/Change Tasks which are there in the system. But i do not want that to happen. Could you please let me know how can I stop that ?
Regards,
Arindam Bose.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2019 04:13 AM
HI,
What i assume is that, whenever you are trying to create a new record duplicate number is getting inserted and hence you are getting issue. SO you want a script which should not allow a insertion of change task/request if the number already exists. IF so then use below script.
//For CHG Request (Before Insert BR)
var gr = new GlideRecord('change_request');
gr.addQuery('number',current.number);
gr.query();
if(gr.next()){
current.setAbortAction(true);
gs.addInfoMessage('Number is Duplicate');
}
Thanks,
Ashutosh