The document describes a trigger on the Lead object that converts leads to opportunities when their status is updated to "Working - Contacted". It includes an Apex test class to test the trigger functionality. The test class creates sample lead records, inserts them, and validates that leads with the correct status are converted to opportunities while others are not. It also includes a bulk test that inserts 50 leads to test conversion at scale.
The document describes a trigger on the Lead object that converts leads to opportunities when their status is updated to "Working - Contacted". It includes an Apex test class to test the trigger functionality. The test class creates sample lead records, inserts them, and validates that leads with the correct status are converted to opportunities while others are not. It also includes a bulk test that inserts 50 leads to test conversion at scale.
The document describes a trigger on the Lead object that converts leads to opportunities when their status is updated to "Working - Contacted". It includes an Apex test class to test the trigger functionality. The test class creates sample lead records, inserts them, and validates that leads with the correct status are converted to opportunities while others are not. It also includes a bulk test that inserts 50 leads to test conversion at scale.
Restricting a lead to convert with status equals to closed to converted?
Trigger Code for Lead:
Trigger AutoConverter on Lead (after insert,after update) { 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.status=='Working - Contacted') { 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); } } Apex Test Class : @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 = 'Working - Contacted'); }
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();
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'); } 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(); 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);