Friday, January 9, 2015

How to instantly see potential duplicates on the Lead page

Over the time company's sales representatives could create Duplicated Leads in Salesforce. Often it creates awkwardness and challenges. For instance when different reps call to the same individual. As a result the company could lose the sale opportunity right from the start.

Salesforce has native “Find Duplicates” button located on the Lead layout but reps not always use it BEFORE the actual call or email to potential buyer. It also time consuming to check out every Lead.

Recently we received following request from one of our Customers: "Is it possible to see duplicates on the lead’s front page (ie without clicking find duplicates)? Sort of like how I can see open activities and activity history without scrolling down. This would save me a click every time on Find Duplicates button to check the same."

So Customer wants from one glance to see if the current Lead has potential duplicates  - without extra clicks. And only click to "Find Duplicates" button for search and merge duplicated Lead if needed.

You can find details of solution we've built below.

Solution to instantly show list of duplicated Leads on the Lead layout.

1) VF page (SeeDuplicatesLeads) to be embedded to Lead Layout. VF page shows list of potential duplicates of the current Lead. List consists of following columns: 
Name | Last Name | Company | Email | Phone | Lead Owner 

2) APEX controller uses similar to native "Find Duplicates" button's matching logic to identify Lead's duplicates (the same Lead Name OR the same Company OR the same Email OR the same Email Domain OR the same Phone).

See code samples below.

Extra note: Solution uses the Dynamic SOQL approach to avoid issues when one or more of the filed(s) used in SOQL have NULL value.

VF page: SeeDuplicatesLeads
<!-- 
Version      : 1.0
Company      : WebSolo Inc.
Date         : 01.2015
Description  : VF page "SeeDuplicatesLeads" to to instantly show list of duplicated Leads on the Lead layout
History      :             
-->
<apex:page standardController="Lead" extensions="SeeDuplicatesLeads">
     <apex:pageBlock >
        <apex:pageBlockTable value="{!LeadList}" var="tl">
            <apex:column headerValue="Name">
                <a href="/{!tl.id}" target="_top" id="{!tl.id}">{!tl.Name}</a>
            </apex:column>            
            <apex:column headerValue="Email" value="{!tl.Email}"/>
            <apex:column headerValue="Company" value="{!tl.Company}"/>
            <apex:column headerValue="Phone" value="{!tl.Phone}"/>
            <apex:column headerValue="Lead Owner" value="{!tl.Owner.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>   
</apex:page>

APEX Class: SeeDuplicatesLeads
/*
Version      : 1.0
Company      : WebSolo inc.
Date         : 01.2015
Description  : controller for VF page "SeeDuplicatesLeads"
History      :             
*/
public class SeeDuplicatesLeads 
{
    public id leadId {get; set;}
    public list<Lead> LeadList {get; set;}
    public SeeDuplicatesLeads(ApexPages.StandardController controller) 
    {
        leadId = ((Lead) controller.getRecord()).id;
        LeadList = new list<Lead>();
        Lead leadobj = [select id, Name, Email, Company, Phone, OwnerId, Owner.Name from Lead where id=: leadId limit 1];
        String sqlStart = 'select id, Name, Email, Company, Phone, OwnerId, Owner.Name from Lead where ';
        String sqlEnd = ' and id !=\'' + leadobj.id + '\'';
        String sqlwhere = '';
        if(leadobj.Email == null)
        {
            if(leadobj.Company == null)
            {
                if(leadobj.Phone == null)
                {
                     sqlwhere = 'Name =\'' + leadobj.Name + '\'';
                }
                else
                {
                     sqlwhere = '(Name =\'' + leadobj.Name + '\' or Phone =\'' + leadobj.Phone + '\')';
                }
            }   
            else
            {
                if(leadobj.Phone == null)
                {
                    sqlwhere = '(Name = \'' + leadobj.Name + '\' or Company =\'' +  leadobj.Company + '\')';
                }
                else
                {   
                    sqlwhere = '(Name = \'' + leadobj.Name + '\' or Company = \'' + leadobj.Company + '\' or Phone = \'' + leadobj.Phone + '\')';   
                }
            }    
        }  
        else
        {
            list<String> EmailDomain = leadobj.Email.split('@');
            list<Lead> LeadListNew = new list<Lead>();          
            if(leadobj.Company == null)
            {
                if(leadobj.Phone == null)
                {
                     sqlwhere = '(Name =\'' + leadobj.Name + '\' or Email LIKE \'' + EmailDomain[1] + '\')';
                }
                else
                {
                     sqlwhere = '(Name =\'' + leadobj.Name + '\' or Phone =\'' + leadobj.Phone + '\' or Email LIKE \'' + EmailDomain[1] + '\')';
                }
            }   
            else
            {
                if(leadobj.Phone == null)
                {
                    sqlwhere = '(Name = \'' + leadobj.Name + '\' or Company =\'' +  leadobj.Company + '\' or Email LIKE \'' + EmailDomain[1] + '\')';
                }
                else
                {   
                    sqlwhere = '(Name = \'' + leadobj.Name + '\' or Company = \'' + leadobj.Company + '\' or Phone = \'' + leadobj.Phone + '\' or Email LIKE \'' + EmailDomain[1] + '\')';   
                }
            }           
        } 
        LeadList = Database.query(sqlStart + sqlwhere + sqlEnd);                                                                                                                                                                                                                                                                                                                    
        if(LeadList.size() > 100)
        {
            list<Lead> goodLeadlist = new list<Lead>();
            Integer Igood = 100;
            for(Integer I = 0; I < Igood; I++)
            {
                goodLeadlist.add(LeadList[I]);
            }
            LeadList.clear();
            LeadList.addall(goodLeadlist);          
        }
    }
}