+ Start a Discussion
pallipalli 

How can I write a SOQL query to find duplicate Account names

hi, 

 

 

 
How can I write a SOQL query to find duplicate Account names.................
 
 
Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

Refer this links :

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm

 

http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/

 

public class DuplicateAccNames
{

public List<AggregateResult> acc{get;set;}
acc=[SELECT Name, count(Id) FROM Account GROUP BY Name HAVING count(Id)>1];
for(AggregateResult ar : acc)
{
System.debug('duplicate name values'+ar);
}

}


}

All Answers

pallipalli

hi ,

 

here my code.......................

 

 

public class DuplicateAccNames
{
public List<Account> acc{get;set;}
public void accMethod()
{
acc=[SELECT Name, count(Id) FROM Account GROUP BY Name HAVING count(Id)>1];
for(Account ac : acc)
{
System.debug('duplicate name values'+ac);
}
}

 

 

error in line 6 ille illegal assignment from List<aggraregate results> to list<account> 

Laxman RaoLaxman Rao

Refer this links :

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm

 

http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/

 

public class DuplicateAccNames
{

public List<AggregateResult> acc{get;set;}
acc=[SELECT Name, count(Id) FROM Account GROUP BY Name HAVING count(Id)>1];
for(AggregateResult ar : acc)
{
System.debug('duplicate name values'+ar);
}

}


}

This was selected as the best answer
vishal@forcevishal@force

This will give you the name of all Accounts that are duplicates:

 

 

List<AggregateREsult> lstAR = [SELECT Name, count(Id) FROM Account GROUP BY Name HAVING count(Id)>1];
for(AggregateResult a : lstAR)
{
         system.debug('=====================   ' + a.get('Name'));
}