2

I have found similar queries in here But nothing gave me a straight answer

I have a custom field on account say custom__C which is a formula field and derives value from another custom field,source__c

I have a trigger ,

trigger apextrigger on Account (before update) {


    system.debug('Trigger Formula field'+trigger.new[0].custom__c);
    system.debug('Trigger Formula field'+trigger.old[0].custom__c);

}

i have a record in DB for a account which looks like below

Source__c = 16; Custom__C = 16; Id = 'abcd%^&*&&'

;

For saving time i have a test class(with see all data true) as below

@istest(seealldata=true)
public class accounttriggerclass {
public static testmethod void testtrigger()
    {
        test.startTest();

        id Idy = 'abcd%^&*&&';//same as the one in DB

        account a = [select name from account where id = :idy limit 1];
        a.source__c = 15;
        update a;
        test.stopTest();
        }

}

Now when i look at debug log i can see

20:02:34.186 (186937531)|USER_DEBUG|[6]|DEBUG|Trigger Formula field15.00
20:02:34.186 (186815519)|USER_DEBUG|[5]|DEBUG|Trigger Formula field16.00

Here can see ,formula field displays the values based on what is available in Trigger.old and Trigger.New is that correct? Because in lot of other discussion it sounded like,only after trigger gets the right value?

  • Sorry I am a bit confused as to what the question is. – jnoel10 Jul 28 '14 at 19:41
2

Formula fields pull data directly out of the database, and generate the value of the field from that when they are queried. So, if the value was, say, 5 + 7 in the database for trigger.old, the formula field would be 12. After the update, if you updated the 7 to a 10, the value of the formula field would be 15 for trigger.new. If you need to make sure you are using the proper, updated value, update your objects and then get the formula field, and the values will be up to date.

Just had a problem with this, and found a LOT of strange answers about it, but that's what I managed to figure out.

| improve this answer | |
  • 2
    "Strange answers" being that formulas used to have strange behaviors. For example, formulas used to render incorrect values when one or more fields in the formula were not also in the query, and they used to not be present in triggers by default, one had to query them to get any value, and even then, they were the "old" value in a before-dml-event trigger, and the "new" value in an after-dml-event trigger. Fortunately, they've mostly ironed out these details, so you should always get the correct value, unless they use a rollup summary field. – sfdcfox Jul 28 '14 at 22:27
0

Because you are using a before update trigger, Trigger.new will contain record(s) reflecting any changes were made (hence 15). However, a query to the database will still have the old value, as the record has not been updated yet.

as sfdcfox says in the comments, Trigger.old contains the old value throughout the transaction, both before and after udate.

This will apply for all calculations as well, including any calculations with dependencies on any other calculations.

If this doesn't fully answer the question, please let me know.

| improve this answer | |
  • Trigger.old always contains the value that existed before the transaction started (e.g. both in before and after event triggers). – sfdcfox Jul 28 '14 at 22:25
  • Good point - will edit my answer to reflect this. – Caspar Harmer Jul 28 '14 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.