Created
March 21, 2021 20:53
-
-
Save niavesper/3dee26bde9c9b1629cd91948af12d57d to your computer and use it in GitHub Desktop.
LeadHandler
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
public with sharing class LeadHandler { | |
public static void handleAfterInsert(List<Lead> newLeads){ | |
List<Task> newTasks = new List<Task>(); | |
for (Lead l : newLeads){ | |
Task t = new Task(); | |
t.WhoId = l.Id; | |
switch on l.ProductInterest__c{ | |
when 'Cookbook Authorship'{ | |
t.Subject = 'Follow up About Cookbook Authorship'; | |
} when 'Cookbook Editing'{ | |
t.Subject = 'Follow up About Cookbook Editing'; | |
} when 'Cookbook Distribution'{ | |
t.Subject = 'Follow up About Cookbook Distribution'; | |
} when else { | |
t.Subject = 'Other'; | |
} | |
} | |
newTasks.add(t); | |
} | |
insert newTasks; | |
} | |
public static void handleAfterUpdate(List<Lead>newLeads, Map<Id, Lead>oldLeadMap){ | |
List<Task> newTasks = new List<Task>(); | |
for (Lead newLead : newLeads){ | |
Lead oldLead = oldLeadMap.get(newLead.Id); | |
if (newLead.ProductInterest__c != oldLead.ProductInterest__c){ | |
Task t = new Task(); | |
t.WhoId= newLead.Id; | |
t.Subject = 'Follow up About Product Interest Change'; | |
newTasks.add(t); | |
} | |
} | |
insert newTasks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tugce Thank you for the great tip. I was going to use nested if statements at first, then opted for a switch to make it a bit more efficient, but your suggestion is much better. Thank you again!