script include with GlideAggregate to find duplicate records based on multiple values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 03:34 AM
Hello
I'm trying to build a report which would filter out records, which have identical values in two different fields. I'm trying to achieve that by a script include, which is called in the report.
Script include:
function getDuplicates() {
var dealsGA = new GlideAggregate('x_deals');
dealsGA.addAggregate('COUNT', 'company_name');
dealsGA.addHaving('COUNT', 'company_name', '>', '1');
dealsGA.query();
var listOfDuplicates = new Array();
while (dealsGA.next()) {
listOfDuplicates.push(dealsGA.getValue('company_name'));
}
return listOfDuplicates;
}
When running the report, it filters out an groups records by company_name field, where there are multiple records having the same company_name.
I want to do the same, but where company_name and code_name are checked. I tried adding addAggregate and addHaving for another field in the same way as with the current one, but that didnt work. Has anyone deal with something similar before?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 04:06 AM
Hi @karolisjon
On your filter, you have to code Script Include as below:
javascript: new ScriptIncludeName().getDuplicates(); //replace ScriptIncludeName() with your actual Script Include name.
Filter should be 'is one of' as it will return list of records:
Company Name is one of javascript: new ScriptIncludeName().getDuplicates();
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 04:19 AM
Thanks @AnubhavRitolia , but how can I adjust the script include itself, so that it would check multiple fields, instead of only one, as it does currently?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 04:25 AM
Hi @karolisjon
In that case you may have to using do nesting like below:
function getDuplicates() {
var dealsGA = new GlideAggregate('x_deals');
dealsGA.addAggregate('COUNT', 'company_name');
dealsGA.addHaving('COUNT', 'company_name', '>', '1');
dealsGA.query();
var listOfDuplicates = new Array();
while (dealsGA.next()) {
var dealsGA1 = new GlideAggregate('x_deals');
dealsGA1.addAggregate('COUNT', 'another_field');
dealsGA1.addHaving('COUNT', 'another_field', '>', '1');
dealsGA1.query();
while(dealsGA1.next())
{
listOfDuplicates.push(dealsGA1.getValue('company_name'));
}
}
return listOfDuplicates;
}
Like this you have to do while nesting for different fields to filter out for multiple fields.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 04:31 AM
Hello @karolisjon
Could you please share your complete requirement, so that it is easy to understand what you are trying to achieve.