This trigger creates a Task for the Opportunity Owner to split revenue when an Opportunity’s Stage is updated to “Closed Won.” It facilitates post-sales follow-up and streamlines task assignment.
Practice similar triggers via this YouTube playlist.
Solution:
public class OpportunityTriggerHandler {
public static void handleActivitiesAfterInsert () {}
public static void handleActivitiesAfterUpdate (List<Opportunity> newRecords) {
List<Task> taskListToInsert = new List<Task> ();
for (Opportunity opp: newRecords) {
if (opp.StageName == ‘Closed Won’) {
//create a related record Task record
Task taskRecord = new Task();
taskRecord.Priority = ‘High’;
taskRecord.OwnerId = opp.OwnerId;
taskRecord.Description = ‘Please split the revenue amongst the team members’;
taskRecord.Status = ‘Not Started’;
taskRecord.Subject = ‘Split Revenue’;
taskRecord.WhatId = opp.Id;
taskListToInsert.add(taskRecord);
}
}
if (!taskListToInsert.isEmpty())
insert taskListToInsert;
}
}