Skip to content

Instantly share code, notes, and snippets.

@niavesper
Created March 21, 2021 20:53
Show Gist options
  • Save niavesper/3dee26bde9c9b1629cd91948af12d57d to your computer and use it in GitHub Desktop.
Save niavesper/3dee26bde9c9b1629cd91948af12d57d to your computer and use it in GitHub Desktop.
LeadHandler
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;
}
}
@tugce
Copy link

tugce commented Mar 24, 2021

Great job @niavesper! 👏 Using switch might be an overkill on this scenario as you can assign subject with 'Follow up ' + lead.Product_Interest__c with one line but other than that looks awesome!

@niavesper
Copy link
Author

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment