Last active
July 11, 2019 02:43
-
-
Save AlwaysThinkin/f27f102d71568a1970c70d171a9368a4 to your computer and use it in GitHub Desktop.
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>(); | |
List<Competitor__mdt> competitorRecords = [Select Label from Competitor__mdt];//Retrieve our Custom Metadata Records | |
List<String> competitorNames = new List<String>();//Instantiate an empty list to hold the names of our competitors | |
for(Competitor__mdt record : competitorRecords){//Loop through the Custom Metadata records our query returned | |
competitorNames.add(record.Label);//Add them to our empty list | |
} | |
for(Integer i = 0 ; i < trigger.new.size() ; i++){ | |
Lead old = trigger.old[i]; | |
Lead nw = trigger.new[i]; | |
if(old.Status != nw.Status && nw.Status == 'Closed - Not Converted'){ | |
Lead myLead = new Lead(Id = nw.Id, Rating = 'Cold'); | |
leadsToUpdate.put(nw.Id, myLead); | |
} | |
//put this inside your Lead Trigger's for loop, after the first if condition ends | |
if(old.Company != nw.Company && competitorNames.contains(nw.Company)){ //Use the .contains() List Method to check if the Company field is in the Competitors Custom Metadata | |
if(leadsToUpdate.containsKey(nw.id)){ | |
leadsToUpdate.get(nw.Id).DoNotCall = True; | |
} else {//If the map doesn't contain the record ID... | |
Lead myLead = new Lead(Id = nw.Id, DoNotCall = true); | |
leadsToUpdate.put(nw.Id, myLead); | |
} | |
} | |
} | |
if(leadsToUpdate.size() > 0){ | |
update leadstoUpdate.values(); | |
} | |
} |
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<String> competitorNames = new List<String>(); | |
public static List<Lead> leadsToInsert = new List<Lead>(); | |
public static void createTestData(){ | |
//Use a for loop to prepare multiple Leads | |
for(Integer i = 0 ; i < 2 ; i++){ | |
Lead myLead = new Lead(); | |
myLead.LastName = 'Lead Last Name' + i; | |
myLead.Company = 'Lead Company' + i; | |
leadsToInsert.add(myLead); | |
} | |
insert leadsToInsert;//Insert our test data | |
for(Competitor__mdt record : [Select Label from Competitor__mdt]){ | |
competitorNames.add(record.Label); | |
} | |
System.debug('from setup' + competitorNames); | |
} | |
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 : 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(){ | |
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 : leadsToInsert){ | |
for(String competitorName : competitorNames){ | |
l.Company = competitorName; | |
} | |
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); | |
} | |
} | |
public static testMethod void testBothRatingDoNotCall(){ | |
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 : leadsToInsert){ | |
for(String competitorName : competitorNames){ | |
l.Status = 'Closed - Not Converted'; | |
l.Company = competitorName; | |
leadsToUpdate.add(l); | |
} | |
} | |
test.startTest();//Start our actual test, updating our records | |
update leadsToUpdate;//issue update call | |
test.stopTest(); | |
List<Lead> testLeads = [Select Rating, 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.assertEquals('Cold', l.Rating); | |
System.assert(l.DoNotCall); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment