Apex Merge Leads and Merge Contacts on Lead Conversion

 public static Map<String, Lead> checkforDuplicateEmailsInContact(Map<String, Lead> mobileLeads)
    {
        Set<String> checkduplicateEmails = new Set<String>();
        checkduplicateEmails.addAll(mobileLeads.keySet());

        Map<String,Contact> contactMap = getContactsMatchingEmailsGiven(checkduplicateEmails);
        Map<String ,List<Lead>> leadMap = getLeadsMatchingEmails(checkduplicateEmails);

        List<Opportunity> createPlanOpportunity = new List<Opportunity>();
        
        legalIssueForId = new Map<Id, String>();

        for (String duplicateEmailLead : mobileLeads.keySet())
        {
            if (leadMap.containsKey(duplicateEmailLead))
            {
                for (Lead lead : leadMap.get(duplicateEmailLead))
                {
                    if (lead.id != mobileLeads.get(lead.Email).id)
                    {
                        merge lead mobileLeads.get(lead.Email);
                        //mobileLeads.remove(duplicateEmailLead);
                    }
                }
            }
        }

        for (String duplicateEmailContact : mobileLeads.keySet())
        {
            if (contactMap.containsKey(duplicateEmailContact))
            {
                if (mobileLeads.get(duplicateEmailContact).LeadSource == 'Mobile FAL')
                {
                    createPlanOpportunity.add(ACRoundRobinV5.CreatePlanOpportunityForMobileOpp(contactMap.get(duplicateEmailContact).AccountId, 'Pro Legal Plan - Annual (A)', 'Mobile FAL', mobileLeads.get(duplicateEmailContact).Legal_Issue__c));   
                    legalIssueForId.put(mobileLeads.get(duplicateEmailContact).Id, mobileLeads.get(duplicateEmailContact).Legal_Issue__c);
                }
                mobileLeads.remove(duplicateEmailContact);
            }
        }
    
        insert createPlanOpportunity;
        return mobileLeads;
    }

    public static Set<Database.LeadConvertResult> convertLeads(Map<String, Lead> leadWithNoExistingEmail)
    {
        Set<Database.LeadConvert> listOflc = new Set<Database.LeadConvert>();
       
        String convertedStatus = getLeadConvertionStatus();
  
        for (Lead leadsToBeConverted : leadWithNoExistingEmail.values())
        {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(leadsToBeConverted.id);
            lc.setDoNotCreateOpportunity(true);
            lc.setConvertedStatus(convertedStatus);
            listOflc.add(lc);
        }
        
        Set<Database.LeadConvertResult> leaConvertion = new Set<Database.LeadConvertResult>();
        for (Database.LeadConvert convertlead : listOflc)
        {
            Database.LeadConvertResult mobileFalLCR = Database.convertLead(convertlead);
            leaConvertion.add(mobileFalLCR);
        }
        return leaConvertion;
    }
Advertisements

One Comment on “Apex Merge Leads and Merge Contacts on Lead Conversion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: