script include with GlideAggregate to find duplicate records based on multiple values

karolisjon
Tera Contributor

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. 

 

comany_name_duplicates.png

 

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.

8 REPLIES 8

AnubhavRitolia
Mega Sage
Mega Sage

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();

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

karolisjon
Tera Contributor

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? 

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.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Mahendra RC
Mega Sage

Hello @karolisjon 

 

Could you please share your complete requirement, so that it is easy to understand what you are trying to achieve.