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 onOpportunity
that updates the records, it might fire before your trigger, causing you to see the new value ofCreditStatus__c
inTrigger.oldMap
before you see old value. Consider the following:But you should see the old value at least once.