Use Case: Web-to-lead form needs to be auto converted to Account, Contact and Opportunity. Create a trigger for the Web-to-lead record type. Check if that contact email already exist. If it does just create an opportunity.
The trigger will look like this:
trigger autoConvertLead on Lead (before insert,after insert, after update) { List<Lead> marketoLeadsToBeConverted = new List<Lead>(); Set<String> emailSet = new Set<String>(); boolean isChanged = false; Lead oldLeadInfo = null; Set<Id> rLDBLeadIdSet = new Set<Id>(); Map<String, Lead> mobileFALMap = new Map<String, Lead>(); for (Lead leadInfo : trigger.new) { isChanged = false; if (trigger.isAfter) { if (!leadInfo.IsConverted) { if (leadInfo.email != null ) { if (Trigger.isInsert) isChanged = true; else if (Trigger.isUpdate) { oldLeadInfo = trigger.oldMap.get(leadInfo.Id); isChanged = Utils.hasChanges('Email', oldLeadInfo, leadinfo); } if (isChanged) { if (leadInfo.RLDB__c) rLDBLeadIdSet.add(leadInfo.Id); if (leadInfo.RecordTypeId == [Select Id from RecordType where Name='Mobile FAL Leads'].Id) mobileFALMap.put(leadInfo.email, leadInfo); else { marketoLeadsToBeConverted.add(leadInfo); emailSet.add(leadInfo.email); } } } } } else { leadInfo.Created_Date__c = System.today(); } } if (marketoLeadsToBeConverted != null && marketoLeadsToBeConverted.size() > 0) { Utils.convertMarketoLeads(marketoLeadsToBeConverted,emailSet); } if (rLDBLeadIdSet != null && rLDBLeadIdSet.size() > 0) { Utils.mergeAndConvertLeads(rLDBLeadIdSet); } if (!mobileFALMap.isEmpty()) Utils.convertMobileFALLeads(mobileFALMap); }
The Apex class to check for duplicate emails, convert lead and create opportunities.
public static void convertMobileFALLeads(Map<String, Lead> mobileFALLeads) { Set<String> checkduplicateEmails = new Set<String>(); checkduplicateEmails.addAll(mobileFALLeads.keySet()); Map<String,Contact> contactMap = getContactsMatchingEmailsGiven(checkduplicateEmails); List<Opportunity> createPlanOpportunity = new List<Opportunity>(); for (String duplicateEmailContact : mobileFALLeads.keySet()) { if (contactMap.containsKey(duplicateEmailContact)) { createPlanOpportunity.add(ACRoundRobinV5.CreatePlanOpportunityForIncorpOpp(contactMap.get(duplicateEmailContact).AccountId, contactMap.get(duplicateEmailContact).Account.OwnerId, 'Pro Legal Plan - Annual (A)', 'Mobile FAL')); mobileFALLeads.remove(duplicateEmailContact); } } Database.LeadConvert lc = new Database.LeadConvert(); List<Database.LeadConvert> listOflc = new List<Database.LeadConvert>(); String convertedStatus = getLeadConvertionStatus(); for (Lead leadsToBeConverted : mobileFALLeads.values()) { lc.setLeadId(leadsToBeConverted.id); lc.setOwnerId(ACRoundRobinV5.getSalesRepsForRouting(ACRoundRobinV5.getListOfActiveLawyerConnectionReps()).Id); lc.setDoNotCreateOpportunity(true); lc.setConvertedStatus(convertedStatus); listOflc.add(lc); } for (Database.LeadConvert convertlead : listOflc) { Database.LeadConvertResult mobileFalLCR = Database.convertLead(convertlead); createPlanOpportunity.add(ACRoundRobinV5.CreatePlanOpportunityForIncorpOpp(mobileFalLCR.getAccountId(), convertlead.getOwnerId(), 'Pro Legal Plan - Annual (A)', 'Mobile FAL')); } insert createPlanOpportunity; }