Do Apex Triggers have access to formula fields?

This will work without having to query the Account object. Formula field values are calculated when read so you'll have the most up to date value available in the trigger.

On a side note, you probably want to change your if statement to have == not =.


A new solution for this was introduced in the Winter '16 release:

yourSObject.recalculateFormulas();

Recalculates all formula fields on an sObject, and sets updated field values. Rather than inserting or updating objects each time you want to test changes to your formula logic, call this method and inspect your new field values. Then make further logic changes as needed.

Documentation link: http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_new_classes_methods.htm


There is a small catch to this formula fields thing. They are evaluated asynchronously, when you read a record(they seem to be evaluated in real time, but actually not). This might cause some side effects in triggers. I never noticed any lags, but it's possible. Check if in your trigger you are getting the previous formula field value.

Tags:

Apex

Trigger