function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dinesh Devaraj 6dinesh Devaraj 6 

i want to show account duplicate names with there count?how can i show in vf page

NagendraNagendra (Salesforce Developers) 
Hi Dinesh,

Please find the sample code for the same on Contact object and tweak it as per the requirement for Account.

Visual Force Page:
<apex:page controller="DuplicateContactsController">
    <apex:pageMessages escape="false"></apex:pageMessages> 
    <apex:form >
        <apex:pageBlock title="Duplicate Contacts">
            <apex:pageBlockTable value="{! contacts}" var="contact">
                <apex:column headerValue="Name" value="contact.Name"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex:
public without sharing class DuplicateContactsController{

     public DuplicateContactsController(){}

     public List<Contact> getContacts(){
     
          List<Contact> contacts = new List<Contact>();
          for(Contact con : [SELECT MIN(Id) oldest, COUNT(Id), AccountId accId, Name FROM Contact WHERE AccountId != null AND Name != null GROUP BY AccountId, Name
HAVING COUNT(Id) > 1 ORDER BY COUNT(Id) DESC LIMIT 1000 ]){
               contacts.add(con);
          }
          return contacts;
     }
}
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 
Deepali KulshresthaDeepali Kulshrestha
Hi Dinesh,
Yes, you can definitely show it in your vf page. Simply try the code below and you will surely get the results.
Vf page:
<apex:page controller="CheckDuplicateController">
    <apex:pageBlock >
    
<apex:repeat value="{!keysForDuplicateRecord}" var="key">
    {!key} &nbsp;
    <apex:outputText value="{!strVsIntMap[key]}"/> <br/>
     </apex:repeat>
    </apex:pageBlock>
</apex:page>

Controller class:
public class CheckDuplicateController {
    public List<Account> accList{get;set;}
    public Map<String,Integer> strVsIntMap{get;set;}//This map will hold the name of accounts and their respective counts.
    public List<String> keysForDuplicateRecord{get;set;} //This List will hold the duplicate account names.
    public Set<String> acName{get;set;}
    public CheckDuplicateController()
    {
        accList=[SELECT name from Account];
        acName= new Set<String>();
       keysForDuplicateRecord =new List<String>();
        strVsIntMap= new Map<String,Integer>();
        for(Account a: accList)
        {
            Integer count=1;
            if(!strVsIntMap.containskey(a.name))  
            {  
                strVsIntMap.put(a.name,count);
            }
            else
            {
                count= strVsIntMap.get(a.name);
                count++;
                strVsIntMap.put(a.name,count);
            }
        }

        for(String  s:strVsIntMap.keySet()){
           Integer value=strVsIntMap.get(s);
            if(value !=1){
                keysForDuplicateRecord.add(s);
            }
        }
        System.debug(':::'+strVsIntMap);
    }
      
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha