This trigger generates a follow-up Task for the Lead Owner when a new Lead record is created. It ensures timely engagement with prospects and automates task assignments.
Explore similar examples in this YouTube playlist.
Solution:
public class LeadTriggerHandler {
public static void handleActivitiesAfterInsert (List<Lead> leadRecords) {
List<Task> taskListToInsert = new List<Task>();
for (Lead leadRec : leadRecords) {
Task taskRecord = new Task();
taskRecord.Priority = ‘High’;
taskRecord.OwnerId = leadRec.OwnerId;
taskRecord.Description = ‘Follow up with the new lead’;
taskRecord.Status = ‘Not Started’;
taskRecord.Subject = ‘Follow Up’;
taskRecord.WhoId = leadRec.Id;
taskListToInsert.add(taskRecord);
}
if(!taskListToInsert.isEmpty()) {
insert taskListToInsert;
}
}
}