Create Task for Opportunity Owner on Stage Change

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;
    }
}

 

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)