Update Opportunity Amount Based on Stage Change

This trigger calculates and updates the Opportunity Amount when the Stage changes. The amount is derived from the Probability and Expected Revenue, ensuring accurate financial projections.

Learn more about triggers from this YouTube playlist.

Solution:

public class OpportunityTriggerHandler {

      public static void handleActivitiesBeforeUpdate (List<Opportunity> newRecords, Map<Id,

Opportunity> oldMap) {

//figure out if stage has been modified or not

//find the record from new list, compare and find the same record in old list and then check if stage

values are differ

            //1 record-Opp1, Opp2, Opp3

           for (Opportunity newOpp : newRecords) {

                 if (oldMap.get (newOpp.Id).StageName != newOpp.StageName) {                                      newOpp.Amount = newOpp.Probability * newOpp.ExpectedRevenue;

               }

         }

  }

  public static void handleActivitiesAfter Update (List<Opportunity> newRecords) {

        List taskListToInsert = new List();

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

       }

   }

}

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 :)