Skip to content

Instantly share code, notes, and snippets.

@asmitakhare
Last active April 13, 2023 12:13
Show Gist options
  • Save asmitakhare/ab295f5de72c9874198430dd27f5c667 to your computer and use it in GitHub Desktop.
Save asmitakhare/ab295f5de72c9874198430dd27f5c667 to your computer and use it in GitHub Desktop.
WeekThreeHomeWork
public with sharing class WeekThreeHomeworkHandler {
//it is public static condition and not returning anything
public static void leadHandler(List<Lead> lstLeads) {
List<Task> listTask = new List<Task>(); // intiating a task list
//writing an if condition where it will create a new task when productInterest picklist has one of the values
//if(l.ProductInterest__c == 'Cookbook Authorship'
//|| l.ProductInterest__c =='Cookbook Editing'
//||l. ProductInterest__c =='Cookbook Distribution')
for (Lead l : lstLeads) {
if (l.ProductInterest__c != null) {
Task task = new Task();
task.subject = 'New Task' + l.ProductInterest__c; //assigning subject name as
task.whoId = l.OwnerId;
task.Status = 'Not Started';
task.priority = 'Normal';
listTask.add(task);
}
}
insert listTask;
}
public static void leadHandlerUpdate(List<Lead> newLeads, Map<Id, Lead>oldLeads){
List<Task> tasksToCreate = new List<Task>();
for (Lead newLead : newLeads) {
Lead oldLead = oldLeadsMap.get(newLead.Id);
if (newLead.Product_Interest__c != null &&
(oldLead == null || newLead.Product_Interest__c != oldLead.Product_Interest__c)) {
Task tasks = new Task();
tasks.Subject = 'New Task' + l.ProductInterest__c;
tasks.Priority = 'Normal';
tasks.Status = 'Not Started';
tasks.WhoId = newLead.ownerId;
tasksToCreate.add(tasks);
}
}
insert tasksToCreate;
}
}
trigger LeadTrigger on Lead (after insert, after update) {
WeekThreeHomeworkHandler lhandler = new WeekThreeHomeworkHandler();
if(Trigger.isInsert){
lhandler.leadHandler(Trigger.new);
}else if (Trigger.isupdate) {
lhandler.leadHandlerUpdate(Trigger.new, Trigger.Old)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment