Last active
August 29, 2015 14:04
-
-
Save pcon/2e555337a30ef72e30c0 to your computer and use it in GitHub Desktop.
Lead Conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Trigger AutoConverter on Lead (after insert) { | |
LeadStatus convertStatus = [ | |
select MasterLabel | |
from LeadStatus | |
where IsConverted = true | |
limit 1 | |
]; | |
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>(); | |
for (Lead lead: Trigger.new) { | |
if (!lead.isConverted && lead.WebForm__c == 'Free Trial') { | |
Database.LeadConvert lc = new Database.LeadConvert(); | |
String oppName = lead.Name; | |
lc.setLeadId(lead.Id); | |
lc.setOpportunityName(oppName); | |
lc.setConvertedStatus(convertStatus.MasterLabel); | |
leadConverts.add(lc); | |
} | |
} | |
if (!leadConverts.isEmpty()) { | |
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
private class AutoConverter_Test { | |
private static Integer LEAD_COUNT = 0; | |
private static Lead createLead() { | |
LEAD_COUNT += 1; | |
return new Lead( | |
FirstName = '_unittest_firstname_: ' + LEAD_COUNT, | |
LastName = '_unittest_lastname_: ' + LEAD_COUNT, | |
Company = '_unittest_company_: ' + LEAD_COUNT, | |
Status = 'Inquiry', | |
WebForm__c = 'Not Free Trial' | |
); | |
} | |
public static void makeFreeTrial(Lead lead) { | |
lead.WebForm__c = 'Free Trial'; | |
} | |
public static List<Lead> fetchLeads(Set<Id> ids) { | |
return [ | |
select isConverted | |
from Lead | |
where Id in :ids | |
]; | |
} | |
public static testMethod void trialConvert() { | |
Lead testLead = createLead(); | |
makeFreeTrial(testLead); | |
Test.startTest(); | |
insert testLead; | |
Test.stopTest(); | |
List<Lead> results = fetchLeads(new Set<Id>{testLead.Id}); | |
System.assertEquals(1, results.size(), 'Did not get the right number of leads back'); | |
System.assert(results.get(0).isConverted, 'The lead should have been converted since it was a "Free Trail"'); | |
} | |
public static testMethod void nonTrialNoConvert() { | |
Lead testLead = createLead(); | |
Test.startTest(); | |
insert testLead; | |
Test.stopTest(); | |
List<Lead> results = fetchLeads(new Set<Id>{testLead.Id}); | |
System.assertEquals(1, results.size(), 'Did not get the right number of leads back'); | |
System.assert(!results.get(0).isConverted, 'The lead should not have been converted since it was not a "Free Trail"'); | |
} | |
public static testMethod void bulkTest() { | |
List<Lead> shouldBeConverted = new List<Lead>(); | |
List<Lead> shouldNotBeConverted = new List<Lead>(); | |
for (Integer i = 0; i < 50; i++) { | |
Lead testLeadNonConvert = createLead(); | |
Lead testLeadConvert = createLead(); | |
makeFreeTrial(testLeadConvert); | |
shouldBeConverted.add(testLeadConvert); | |
shouldNotBeConverted.add(testLeadNonConvert); | |
} | |
List<Lead> toInsert = new List<Lead>(); | |
toInsert.addAll(shouldBeConverted); | |
toInsert.addAll(shouldNotBeConverted); | |
Test.startTest(); | |
insert toInsert; | |
Test.stopTest(); | |
Map<Id, Lead> expectedConversions = new Map<Id, Lead>(shouldBeConverted); | |
Map<Id, Lead> expectedNonConversions = new Map<Id, Lead>(shouldNotBeConverted); | |
Set<Id> leadIds = new Set<Id>(); | |
leadIds.addAll(expectedConversions.keySet()); | |
leadIds.addAll(expectedNonConversions.keySet()); | |
for (Lead result: fetchLeads(leadIds)) { | |
if (expectedConversions.containsKey(result.Id)) { | |
System.assert(result.isConverted, 'This lead should have been converted ' + result); | |
expectedConversions.remove(result.Id); | |
} else if (expectedNonConversions.containsKey(result.Id)) { | |
System.assert(!result.isConverted, 'This lead should not have been converted ' + result); | |
expectedNonConversions.remove(result.Id); | |
} else { | |
System.assert(false, 'We got a Lead we did not expect to get back ' + result); | |
} | |
} | |
System.assert(expectedConversions.isEmpty(), 'We did not get back all the converted leads we expected'); | |
System.assert(expectedNonConversions.isEmpty(), 'We did not get back all the non converted leads we expected'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment