1

I am trying to match Lead based on email address and Phone.
If there is a match found then mark the status of lead as duplicate and owner should be same as existing lead owner.
I have written the below method and it is working for Email field.
What is the best way to do it for Phone if Email field is blank? Should I create one more map for Phone as key.
PS: I am a beginner in Programming. Thanks in Advance.

 private void checkDuplicateLeads() {
    Set<String> EmailSet = new Set<String>();
    Map<String,Lead> ExistingLeadMap = new Map<String,Lead>();
    for(Lead l : (List<Lead>) Trigger.new) {
        if (l.Email != null){
             EmailSet.add(l.Email);
        }
    }
    for (Lead l2 : [SELECT Email,OwnerId,Owner.Type,Phone FROM Lead WHERE Email IN :EmailSet]) {
        ExistingLeadMap.put(l2.Email, l2);
    }
    for(Lead l3: (List<Lead>) Trigger.new){

        if(ExistingLeadMap.containsKey(l3.Email)){
            l3.Status='Duplicate Lead';
            if(ExistingLeadMap.get(l3.Email).Owner.Type=='User'){
                l3.OwnerId=ExistingLeadMap.get(l3.Email).Ownerid;
            }
        }
    }
}
  • Have you looked at the standard Duplicate Management feature? – Adrian Larson May 19 '16 at 14:40
  • @AdrianLarson, Leads will be inserted through API/Web-to-Leads. Business wants to allow the creation of leads (we don't want to prevent duplicate) and assign it to same owner as existing leads. – C0DEPirate May 19 '16 at 15:08
1

You are correct that you need another map. You should add a filter on Status to your query.

Here are a couple helper methods that can clean up your code a bit.

public static final String DUPE_STATUS = 'Duplicate Lead';
public static Set<String> pluckStrings(List<Lead> records, SObjectField field)
{
    Set<String> values = new Set<String>();
    for (Lead record : records) values.add((String)record.get(field));
    values.remove(null);
    return values;
}
static void checkDuplicate(Lead original, Lead candidate)
{
    if (candidate == null) return;
    original.Status = DUPE_STATUS;
    if (candidate.Owner.Type == 'User')
        original.OwnerId = candidate.OwnerId;
}

Then you can make your method:

public static void checkDuplicateLeads(List<Lead> records)
{
    Map<String, Lead> emailToLead = new Map<String, Lead>();
    Map<String, Lead> phoneToLead = new Map<String, Lead>();
    for (Lead record : [
        SELECT Email, Phone, OwnerId, Onwer.Type FROM Lead
        WHERE Status != DUPE_STATUS
        AND (
            Email IN :pluckStrings(records, Lead.Email)
            OR Phone IN :pluckStrings(records, Lead.Phone)
        )
    ]){
        emailToLead.put(record.Email, record);
        phoneToLead.put(record.Phone, record);
    }
    for(Lead record : records)
    {
        if (emailToLead.containsKey(record.Email))
            checkDuplicate(record, emailToLead.get(record.Email));
        else
            checkDuplicate(record, phoneToLead.get(record.Phone));
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.