[SalesForce] Trigger.new and Trigger.Old Map hold same values for a field on Opportunity

I have a trigger on Opportunity which calls a handler and Handler calls an apex class. here is the code:

//Trigger
trigger MasterOpportunityTrigger on Opportunity (
before insert, after insert, 
before update, after update, 
before delete, after delete, after undelete) 
{

  OppoHandler Ohandler = new OppoHandler();

  /* After Update */
  if (trigger.isUpdate && trigger.isAfter) 
  {
      Ohandler.afterUpdate(trigger.old, trigger.oldMap, trigger.new, trigger.newMap);
  }
}


//Handler
public class OppoHandler extends GHandler 
{

    public void afterUpdate(List<Opportunity> originalOpportunity, 
                            Map<ID, Opportunity>  originalOpportunityMap,
                            List<Opportunity> newOpportunity, 
                            Map<ID, Opportunity>  newOpportunityMap) 
    {
        OpportunityProcessHandler ProcO = new OpportunityProcessHandler(originalOpportunityMap, newOpportunity);

    }
}    

      //this class is on after update on opportunity

        public class OpportunityProcessHandler
        {
            private Static Boolean ControlRun = false;
            private List<Opportunity> filteredopplst = New List <Opportunity> ();

            //passed from handler(tigger.oldmap and Trigger.new list)
            public OpportunityProcessHandler(Map <Id, Opportunity > OppoOldMap, List <Opportunity> newOppo)
            {
                if(!ControlRun)
                {
                    ControlRun = True;
                    for (Opportunity Op : newOppo)
                    {
                        System.debug(OppoOldMap.Get(Op.Id).CreditStatus__c +'---'+ Op.CreditStatus__c);

                        //check if opportunity credit status goes from hold to active - then proc the opp
                        if((OppoOldMap.Get(Op.Id).CreditStatus__c != Op.CreditStatus__c) && Op.CreditStatus__c == 'Active'&& Op.StageName == 'Closed Won')
                        {
                            filteredopplst = Oppolst;
                        }
                    }

                    if(filteredopplst.size() > 0 && filteredopplst != null)
                    {
                     OpportunityProvisioner(originalListOppo);
                    }
                }
            }
            public static void OpportunityProvisioner(List <Opportunity> OpptoworkOn)
            {

                //start processing the opportunity

            }
        }

The issue – the Old map of the opportunity for credit status is returning same values as trigger.new opportunity. when the credit status is changed from other values to "Active"

debug result – USER_DEBUG|[14]|DEBUG|Active—Active

What might cause this type behaviour?

Best Answer

If there is another after update trigger on Opportunity that updates the records, it might fire before your trigger, causing you to see the new value of CreditStatus__c in Trigger.oldMap before you see old value. Consider the following:

trigger AnotherTrigger on Opportunity (after update) {
  List<Opportunity> updates = new List<Opportunity>();
  for (Opportunity o: Trigger.new) {
    updates.add(new Opportunity(Id = o.Id, Another_Field__c = 100));
  }
  update updates;
}

But you should see the old value at least once.

Related Topic