14

On the Contact object I have a custom formula field call Prioirity__c that is defined as:

IF(ISPICKVAL(Account.Rating, 'Very Hot'), 'High', '') 

I also have a trigger defined for the contact object as follows:

trigger testFormula on Contact (before update) {
    for( Contact c : Trigger.new ) {
        if( c.Priority__c == 'High' ) {
            // do something
        }
    }
}

My question is will this trigger "do something" with Contact records where the Account.Rating == 'Very Hot' or do I have to run a SOQL query to get the value of Account.Rating for the Contact records being processed by the Trigger?


Edit:

After testing it does appear Apex Triggers have access to formula fields. Does anyone know of where this might be documented? I would like to verify this is indeed as intended and not something that could change in the future.

6 Answers 6

18

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 =.

3
  • Thanks for the note about using == instead of = in the if statement. I have updated my question.
    – sfelf
    Feb 6, 2013 at 2:12
  • Any idea if Apex triggers having access to formula feeds are documented some where? I asked this question here because my tests proved this was true but I had been told in the past that they don't have access to formula fields values.
    – sfelf
    Feb 6, 2013 at 2:14
  • Can you clarify what "when read" means? Is that when the record is queried from the database or when the formula field is used in apex? Is the value available in before insert triggers? Jul 15, 2021 at 1:33
9

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

1
4

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.

3

Before triggers will not have the formula field value available.

1
  • 2
    Not true, at least not as of July 2020. I just re-tested and before insert has access to the formula field value, and before update will have both the old and new values if that formula changed.
    – Sam Howle
    Jul 14, 2020 at 17:34
1

As long as your formula does not contain cross-object references they are available in a before trigger context.

-2

Yes Trigger have access to formula field, i use it a lot.

1
  • 4
    Welcome to SFSE Yann. Your answer might be correct, but it's not helpful. Although the question looks like a yes/no question, we expect the answers to give details and examples, and to have a benefit for other future questions. If you can elaborate on your answer more, give details, references etc. it will be get more positive attention.
    – Saariko
    Sep 3, 2013 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .