2

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?

  • add the trigger code too – Santanu Boral Jan 16 '18 at 0:54
  • Can you post your trigger code? Also, do you have any other automations on Opportunity (Workflow Rules, Process Builder, etc)? – Bob Lopez Jan 16 '18 at 0:55
  • added the trigger code, @BobLopez yes I have 30+ WFRs firing on Opportunity. – Danny Jan 16 '18 at 1:02
  • Where are you populating oppoldmap? Maybe I missed it – Eric Jan 16 '18 at 5:33
  • 1
    (1) If you have WFR that do field updates, be aware that Trigger.old will not be the value of the record after the first pass through the triggers have executed - it will be the original trigger.old when transaction started; (2) Your recursion control static vbl needs to be thought thru carefully when the transaction has > 200 records if such use case arises. – cropredy Jan 16 '18 at 6:46
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.