function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zach AlexanderZach Alexander 

Triggers: Old Value of Formula Field (Works... but shouldn't?)

Hello Everyone,

We have a trigger that works... but I'm concerned it is only by accident and may break in the future. Basically, the trigger utilizes the old value of a formula field called Status Code on an object called Vehicle Inventory; this trigger records the old value of Status Code and the new value of Status Code in a child object called Journal Entries.

My problem is that... well... it seems the order of execution should leave no "old" value of the formula field.

The reason for suspecting this might be a problem is because the Status Code formula is linked cross object to a junction object called Lease Tool... but the trigger itself is on Vehicle Inventory.

User-added image

The data flow starts on an unrleated object called "Lease", which contains a picklist field that tracks the Status of a vehicle (Leased, Repair, etc.). Any changes to this Status picklist field are transported down to a junction object called "Lease Tool" via trigger. At this point, the junction object has several filtered roll-up summaries linked up with Vehicle Inventory. Finally, the formula field Status Code (on Vehicle Inventory) reads the rollup-summary results to produce a value.

Lease --> (Trigger) --> Lease Tool --> (Rollup Summary) --> Vehicle Inventory --> (Trigger in Question) 

The final step is that our trigger on Vehicle Inventory records the "old" and "new" values of the formula field by creating a child object.

I'm amazed that this trigger works! So, for example, if the Picklist status on Lease previously read "Available", and later read "Leased", then the end result is a Journal Entry reading the following:

Previous Status: Availalbe
New Status: Leased
Time of Change: System Time

Does this seem correct? My understanding is that formulas are created at the time of query (when a record is accessed). How is it maintaining an "old" value all the way through this chain of events?

Is it a fluke? Or is this actual Salesforce functionality that we can trust to work in the future? There is an unrelated trigger on the origin object (Lease) that causes an Edit/Save on vehicle inventory at the exact same time the changes to the original Status picklist have been made. So that explains the DML operation on Vehicle Inventory. But... it just doesn't seem that the old value of the formula should survive.

Am I missing something obvious?