Apex Code: Auto Convert Web-to-Lead Check Duplicates Create Opportunity

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;
 }

Web-to-Lead Lead Not being Created – Capture Page: Not available.

If you Web-to-Lead form is not creating leads you may want to enable debugging in your form. Add the following lines to your html page:

<input type="hidden" name="debug" value=1>
<input type="hidden" name="debugEmail" value="youremailaddress@email.com">

When you click submit you may get the following message:

Reason: Your Lead could not be processed.
null
    Lead Capture Page: Not available.

Record Information:

    00Ng0000000OIjv = asdasdasdsad
    city = Test City
    company = Test Company
    debug = 1
    debugEmail = tmichels@rocketlawyer.com
    email = testmail@testmail.com
    encoding = UTF-8
    first_name = Test Name
    last_name = Test Last Name
    lead_source = Employee Referral
    oid = 00Dg00000008e4p
    phone = 4084213722
    recordType = 012g00000004IkA
    retURL = http://
    state = Test State
    submit = Submit

Solution:

Change your action URL by specifying test for Sandbox or your instance name for Production eg: na4
Sandbox will look like this:
<form action="https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

Production will look like this:
<form action="https://na4.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">