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
Mona KawaleMona Kawale 

//If I am inserting account then it should make count with //respect to its number of same account with same name count in Org.

//If I am inserting account then it should make count with 
//respect to its number of same account with same name count in Org.
public class CountDuplicateAccount {
    
    
    public static void myMethod(List<Account> newList){
        integer count=0;
        set<id> ids=new set<id>();
        for(account ac:newlist){
            ids.add(ac.id);
        }
        map<id,Account> oldlist = new map<id, account>([select id, name from Account where id In: ids]);
        
        if(!newList.isempty()){
            for(Account acc:newList){
                if(acc.name==oldlist.get(acc.id).name)
                    count=count+1;
                acc.Total_Duplicate_Account__c=count+1;
                
                
                
            }
        }
        
        
    }
    
}
PriyaPriya (Salesforce Developers) 
Hi Mona,

By using the below code you can show the count of duplicate Accounts on Visualforce Page. Here is the logic :-

VF Page :- 
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);
    }
      
}
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan


 
Mona KawaleMona Kawale
@Priya Can you please share the code without including VF code. Sorry but I am new to salesforce so it gets difficult to understand VF logic here.
PriyaPriya (Salesforce Developers) 
Hey Mona,

Try this :-
List<AggregateResult> acc=[SELECT Name, count(Id) FROM Account GROUP BY Name HAVING count(Id)>1];
for(AggregateResult result : acc)
{
System.debug('Finding duplicate names'+result);
}
Reference :- https://developer.salesforce.com/forums/?id=9060G0000005l2yQAA


Thanks