Last active
June 22, 2019 13:43
-
-
Save AlwaysThinkin/c5732c8ff76193574f7545cf00bd5355 to your computer and use it in GitHub Desktop.
Guided exercise to create a comparison trigger on Lead to set Rating = Cold on Closed - Not Converted Leads
This file contains 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 | |
public class LeadTriggerTest { | |
public static testMethod void testOneClosedNotConverted(){ | |
Lead l = new Lead(); | |
l.LastName = 'Lead Last Name'; | |
l.Company = 'Lead Company'; | |
insert l; | |
test.startTest(); | |
l.Status = 'Closed - Not Converted'; | |
update l; | |
test.stopTest(); | |
//Retrieve the new Status value for the updated Lead | |
List<Lead> testLeads = [Select Rating from Lead]; | |
//and Assert that it has the value we expect | |
System.assertEquals('Cold', testLeads[0].Rating); | |
//Another way to do the same thing when there's only 1 record you want to test | |
Lead testLead = [Select Rating from Lead Limit 1]; | |
System.assertEquals('Cold',testLead.Rating); | |
} | |
} |
This file contains 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 LeadTrigger on Lead (After Update) { | |
//Create a Map of ID to Lead to hold eventual updates | |
for(){ //Create a for-loop that increments an Integer for each item in trigger.new | |
//Instantiate a Lead from trigger.old | |
//Instantiate a Lead from trigger.new | |
if(){ //Create an if condition that checks if Status changed AND that the new status is Closed - Converted | |
//Instantiate the Lead in the loop for editing, and set Rating = Cold | |
//Put the Lead into your map | |
} | |
} | |
if(){ //Use an if condition to check if there are records in your Map | |
//Issue an update call to save the edited Leads in your Map | |
} | |
} |
This file contains 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 | |
public class LeadTriggerTest { | |
public static testMethod void testManyClosedNotConverted(){ | |
List<Lead> leadsToInsert = new List<Lead>();//Create a List to hold test data we'll add | |
//Use a for loop to prepare multiple Leads | |
for(Integer i = 0 ; i < 2 ; i++){ | |
Lead l = new Lead(); | |
l.LastName = 'Lead Last Name' + i; | |
l.Company = 'Lead Company' + i; | |
leadsToInsert.add(l); | |
} | |
insert leadsToInsert;//Insert our test data | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : leadsToInsert){ | |
l.Status = 'Closed - Not Converted'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select Rating from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assertEquals('Cold',l.Rating); | |
} | |
} | |
} |
This file contains 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
//put this inside your Lead Trigger's for loop, after the first if condition ends | |
if(){ //Create an if condition that checks if Company changed and that the new value is Our Competitor, Inc. | |
if(){//Create an if condtion that checks your map to see if it already contains the record Id | |
leadsToUpdate.get(nw.Id).DoNotCall = True;//If it does, you can get the Do Not Call field, and set it to True | |
} else {//If the map doesn't contain the record ID... | |
//Instantiate the Lead in the loop for editing, and set Do Not Call = True | |
//Put the lead into your map | |
} | |
} |
This file contains 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 | |
public class LeadTriggerTest { | |
public static testMethod void testManyClosedNotConverted(){ | |
List<Lead> leadsToInsert = new List<Lead>();//Create a List to hold test data we'll add | |
//Use a for loop to prepare multiple Leads | |
for(Integer i = 0 ; i < 2 ; i++){ | |
Lead l = new Lead(); | |
l.LastName = 'Lead Last Name' + i; | |
l.Company = 'Lead Company' + i; | |
leadsToInsert.add(l); | |
} | |
insert leadsToInsert;//Insert our test data | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : leadsToInsert){ | |
l.Status = 'Closed - Not Converted'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select Rating from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assertEquals('Cold', l.Rating); | |
} | |
} | |
public static testMethod void testManyDoNotCall(){ | |
List<Lead> leadsToInsert = new List<Lead>();//Create a List to hold test data we'll add | |
//Use a for loop to prepare multiple Leads | |
for(Integer i = 0 ; i < 2 ; i++){ | |
Lead l = new Lead(); | |
l.LastName = 'Lead Last Name' + i; | |
l.Company = 'Lead Company' + i; | |
leadsToInsert.add(l); | |
} | |
insert leadsToInsert;//Insert our test data | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : leadsToInsert){ | |
l.Company = 'Our Competitor, Inc.'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select DoNotCall from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assert(l.DoNotCall); | |
} | |
} | |
} |
This file contains 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 | |
public class LeadTriggerTest { | |
public static list<Lead> testData = new List<Lead>(); | |
public static void createTestData(){ | |
List<Lead> leadsToInsert = new List<Lead>();//Create a List to hold test data we'll add | |
//Use a for loop to prepare multiple Leads | |
for(Integer i = 0 ; i < 2 ; i++){ | |
Lead l = new Lead(); | |
l.LastName = 'Lead Last Name' + i; | |
l.Company = 'Lead Company' + i; | |
testData.add(l); | |
} | |
insert testData;//Insert our test data | |
} | |
public static testMethod void testManyClosedNotConverted(){ | |
createTestData(); | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : testData){ | |
l.Status = 'Closed - Not Converted'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select Rating from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assertEquals('Cold', l.Rating); | |
} | |
} | |
public static testMethod void testManyDoNotCall(){ | |
createTestData(); | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : testData){ | |
l.Company = 'Our Competitor, Inc.'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select DoNotCall from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assert(l.DoNotCall); | |
} | |
} | |
} |
This file contains 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 | |
public class LeadTriggerTest { | |
@testSetup | |
public static void createTestData(){ | |
List<Lead> leadsToInsert = new List<Lead>();//Create a List to hold test data we'll add | |
//Use a for loop to prepare multiple Leads | |
for(Integer i = 0 ; i < 2 ; i++){ | |
Lead l = new Lead(); | |
l.LastName = 'Lead Last Name' + i; | |
l.Company = 'Lead Company' + i; | |
leadsToInsert.add(l); | |
} | |
insert leadsToInsert;//Insert our test data | |
} | |
public static testMethod void testManyClosedNotConverted(){ | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : [Select Status from Lead]){ | |
l.Status = 'Closed - Not Converted'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select Rating from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assertEquals('Cold', l.Rating); | |
} | |
} | |
public static testMethod void testManyDoNotCall(){ | |
List<Lead> leadsToUpdate = new List<Lead>();//Create a List to update our leads in bulk | |
//Use a for loop to prepare our updates | |
for(Lead l : [Select Company from Lead]){ | |
l.Company = 'Our Competitor, Inc.'; | |
leadsToUpdate.add(l); | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select DoNotCall from Lead];//Query for Leads, getting the field updated by our Lead Trigger | |
//Use a for loop to check each Lead | |
for(Lead l : testLeads){ | |
System.assert(l.DoNotCall); | |
} | |
} | |
} |
This file contains 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 LeadTrigger on Lead (After Update) { | |
Map<ID, Lead> leadsToUpdate = new Map<ID, Lead>();//Create a Map to hold eventual updates | |
//This is a very common pattern for iterating over the list of records | |
//We use the size() method for Lists to handle each Lead in Trigger.new | |
for(Integer i = 0 ; i < trigger.new.size() ; i++){ | |
Lead old = trigger.old[i]; | |
Lead nw = trigger.new[i];//We cannot use "new" as our variable name, it's a reserved word. | |
if(old.Status != nw.Status && nw.Status == 'Closed - Not Converted'){ | |
Lead l = new Lead(Id = nw.Id, Rating = 'Cold'); | |
leadsToUpdate.put(nw.Id, l); | |
} | |
if(old.Company != nw.Company && nw.Company == 'Our Competitor, Inc.'){ | |
if(leadsToUpdate.containsKey(nw.id)){ | |
leadsToUpdate.get(nw.Id).DoNotCall = True; | |
} else { | |
Lead l = new Lead(Id = nw.Id, DoNotCall = True); | |
leadsToUpdate.put(nw.Id, l); | |
} | |
} | |
} | |
//BEST PRACTICE: Use size() again to check that you have records to be updated first | |
if(leadsToUpdate.size() > 0){ | |
update leadsToUpdate.values(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment