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
Mahesh Dhara 9Mahesh Dhara 9 

How to Display all duplicate leads based on email and If there r 3 duplicate leads on same email, u show it only once in the table

Best Answer chosen by Mahesh Dhara 9
BALAJI CHBALAJI CH
Can you please try below code:

VF Page:
<apex:page controller="PracticeClass1" tabStyle="Contact">
    <apex:form id="frm" >
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!DuplicateLeadsDisp}" var="dup" >
                    <apex:column headerValue="Name" value="{!dup.Name}" />
                    <apex:column headerValue="Email" value="{!dup.Email}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:commandButton value="BasedOnEmail" action="{!BasedOnEMail}" reRender="frm" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class PracticeClass1
{
    public list<Lead> DuplicateLeadsDisp {get;set;}
    Map<string, integer> Map1 = new Map<string, integer>();
    public PracticeClass1()
    {
        DuplicateLeadsDisp = new list<Lead>();
        for(Lead l : [select id, Name, Email from Lead])
        {
            if(Map1.containsKey(l.Email))
            {
                Integer inte;
                inte = (Map1.get(l.Email))+1;
                Map1.put(l.Email, inte);
            }
            else
                Map1.put(l.Email, 1);
        }
        
        for(Lead l : [select id, Name, Email from Lead])
        {
            if(Map1.get(l.email) > 1)
                DuplicateLeadsDisp.add(l);
        }
    }
    
    public void BasedOnEmail()
    {
        DuplicateLeadsDisp.clear();
        Set<String> Emails = new Set<String>();
        for(Lead l : [select id, Name, Email from Lead])
        {
            if(Map1.get(l.email) >= 3 && !Emails.contains(l.Email))
            {
                DuplicateLeadsDisp.add(l);
                Emails.add(l.Email);
            }
            else if(Map1.get(l.email) == 2)
                DuplicateLeadsDisp.add(l);
        }
    }
}

​Let me know if that works for you.

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Mahesh Dhara,

May I request you to please refer the below link for reference. Hope it will be helpful.

Regards
Rahul Kumar
Mahesh Dhara 9Mahesh Dhara 9
I dont want to use any 3rd party tools through code only i want to do first of all how to get all duplicate leads based on email 
Lokesh KumarLokesh Kumar
Hi Mahesh,

Can you share your code here?

Thanks,
Lokesh
 
BALAJI CHBALAJI CH
Hi Mahesh,

If I'm not wrong, you want to display list of Lead Emails which are duplicates in Lead records. Please below VF Page and Controller Which dipslays Lead Emails and their count used in Lead records.

VF Page:
<apex:page controller="PracticeClass" tabStyle="Contact">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!DuplicateLeads}" var="dup" >
                    <apex:column headerValue="Email" value="{!dup.email1}" />
                    <apex:column headerValue="Count" value="{!dup.no1}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class PracticeClass
{
    public list<WrapperClass> DuplicateLeads {get;set;}
    Map<string, integer> Map1 = new Map<string, integer>();
    public PracticeClass()
    {
        DuplicateLeads = new list<WrapperClass>();
        for(Lead l : [select id, Email from Lead])
        {
            if(Map1.containsKey(l.Email))
            {
                Integer inte;
                inte = (Map1.get(l.Email))+1;
                Map1.put(l.Email, inte);
            }
            else
            Map1.put(l.Email, 1);
        }
        
        list<WrapperClass> DupLeads = new List<WrapperClass>();
        for(String s : Map1.keySet())
        {
                WrapperClass abc = new WrapperClass(s, Map1.get(s));
                system.debug('abc - '+abc);
                DuplicateLeads.add(abc);
        }
    }
        
    public class WrapperClass{
        public string email1{set;get;}
        public integer no1{set;get;}
        
        public WrapperClass(string email, integer no)
        {
            email1 = email;
            no1 = no;
        }   
    }
}

You can k​eep conditions to display if there are 3 duplicate leads with same email like below:
Controller:
public class PracticeClass
{
    public list<WrapperClass> DuplicateLeads {get;set;}
    Map<string, integer> Map1 = new Map<string, integer>();
    public PracticeClass()
    {
        DuplicateLeads = new list<WrapperClass>();
        for(Lead l : [select id, Email from Lead])
        {
            if(Map1.containsKey(l.Email))
            {
                Integer inte;
                inte = (Map1.get(l.Email))+1;
                Map1.put(l.Email, inte);
            }
            else
            Map1.put(l.Email, 1);
        }
        
        list<WrapperClass> DupLeads = new List<WrapperClass>();
        for(String s : Map1.keySet())
        {
            if(Map1.get(s) > 3)
            {
                WrapperClass abc = new WrapperClass(s, Map1.get(s));
                system.debug('abc - '+abc);
                DuplicateLeads.add(abc);
            }
        }
    }
        
    public class WrapperClass{
        public string email1{set;get;}
        public integer no1{set;get;}
        
        public WrapperClass(string email, integer no)
        {
            email1 = email;
            no1 = no;
        }   
    }
}

Let us know if that helps you.

Best Regards,
BALAJI​
Mahesh Dhara 9Mahesh Dhara 9
count should not display and with email remain fields also display in vf page
BALAJI CHBALAJI CH
You mean to say that all duplicate Lead records to be displayed in the VF or one record for each duplicate email ?
"If there r 3 duplicate leads on same email, u show it only once in the table" - Which Lead record to be displayed ?
For Example., there are 4 leads Lead1, Lead2, Lead3, Lead4 with same email., then which lead should be displayed in the VF.., all the four or any particular criteria like last created lead .?
Mahesh Dhara 9Mahesh Dhara 9
1)when page load display lead records like below screen shot only the lead with same email records.

2)when i click the butten BasedOnEmail (If there r 3 duplicate leads on same email, u show it only once in the table )


That is my exact scenario how can it possible,

User-added image
BALAJI CHBALAJI CH
Do you have a screenshot or rough view for second point i.e., to show only once if there are 3 duplicate leads with same email..
Mahesh Dhara 9Mahesh Dhara 9
suppose lead1,lead2,lead3 is having same email display only once any one of lead
Mahesh Dhara 9Mahesh Dhara 9
if mail id is 3 times then display any one of lead rem dont displya and if mail id is 2 times then display both leads only maild is greater than or equal 3 times then only display any one of lead other wise display all duplicate leads based on email
BALAJI CHBALAJI CH
Can you please try below code:

VF Page:
<apex:page controller="PracticeClass1" tabStyle="Contact">
    <apex:form id="frm" >
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!DuplicateLeadsDisp}" var="dup" >
                    <apex:column headerValue="Name" value="{!dup.Name}" />
                    <apex:column headerValue="Email" value="{!dup.Email}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:commandButton value="BasedOnEmail" action="{!BasedOnEMail}" reRender="frm" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class PracticeClass1
{
    public list<Lead> DuplicateLeadsDisp {get;set;}
    Map<string, integer> Map1 = new Map<string, integer>();
    public PracticeClass1()
    {
        DuplicateLeadsDisp = new list<Lead>();
        for(Lead l : [select id, Name, Email from Lead])
        {
            if(Map1.containsKey(l.Email))
            {
                Integer inte;
                inte = (Map1.get(l.Email))+1;
                Map1.put(l.Email, inte);
            }
            else
                Map1.put(l.Email, 1);
        }
        
        for(Lead l : [select id, Name, Email from Lead])
        {
            if(Map1.get(l.email) > 1)
                DuplicateLeadsDisp.add(l);
        }
    }
    
    public void BasedOnEmail()
    {
        DuplicateLeadsDisp.clear();
        Set<String> Emails = new Set<String>();
        for(Lead l : [select id, Name, Email from Lead])
        {
            if(Map1.get(l.email) >= 3 && !Emails.contains(l.Email))
            {
                DuplicateLeadsDisp.add(l);
                Emails.add(l.Email);
            }
            else if(Map1.get(l.email) == 2)
                DuplicateLeadsDisp.add(l);
        }
    }
}

​Let me know if that works for you.
This was selected as the best answer
Mahesh Dhara 9Mahesh Dhara 9
Table size is 25.

Put next and prev buttons, next should show duplicate leads frm 26 to 50.. and so on
Same for prev how can add this to above program