+ Start a Discussion
louisa barrett 7louisa barrett 7 

Before trigger and roll up summary fields

I was hoping someone could explain to me why the below trigger is working.
My test class for the below trigger is failing, which I understand why. Roll Up summaries are calculated once the after trigger has run, so in my before tiggers the values have not yet been updated.
What I don't understand though is why the trigger is working though.
The field 'Hardware_Maintenance_Total__c' is a formula field based on a roll up summary and 2 standard currency field.
The trigger simply increments the 'Change_Revision__c'  value by 1 if the hardware total is changed

Method called by before trigger
private void updateChangeRevision(Map<ID, Opportunity> oldOppsMap, Opportunity[] opps)
        for(Opportunity opp : opps)
            Opportunity oldOpp = oldOppsMap != null ? oldOppsMap.get(opp.Id) : null;
            system.debug('New Hardware maintenance total = ' + opp.Hardware_Maintenance_Total__c + 
                             ' Old Hardware maintenance total = ' + oldOpp.Hardware_Maintenance_Total__c);
            if(oldOpp != null &&
               opp.Hardware_Maintenance_Total__c != oldOpp.Hardware_Maintenance_Total__c)
                opp.Change_Revision__c = opp.Change_Revision__c != null ? opp.Change_Revision__c +1 : 1;

Debug when ran changing values in the record page itself
11:12:40.3 (61046491)|USER_DEBUG|[90]|DEBUG|New Hardware maintenance total = 43.72 Old Hardware maintenance total = 21.33

Debug when running test class
11:44:29.779 (7880532817)|USER_DEBUG|[78]|DEBUG|New Hardware maintenance total = 21.33 Old Hardware maintenance total = 21.33

As I say, I understand why the test class is failing, the roll up summaries will not have been calculated as yet, so the formula field I am checking 'Hardware_Maintenance_Total__c' will not have been updated at that point.
So why is the trigger actually working?

Many thanks