5

I am struck with a nasty situation : I will try to explain this as clear as possible.

I have 2 fields

A & B

B = B + A

when I change A(input field) I want to include the formula's new value and divide the new value by another field

in short : C = A/ B(calculated using formula field)

I want my trigger to include the new value of the formula when i save the record :(!!! Its kind of a chicken and hen story

Until the formula is saved the trigger calculations are not accurate

Looking at this post the post says formula changes are accessible to trigger : Do Apex Triggers have access to formula fields?

Is the above statement true if so am i missing something? I tried to exclude the formula field from the soql but it errors out that soql was excluded (as per the post reference the field need not be included in the soql).

Code :

          for(quote q:[select id,recordtype.id,formula__c from quote where ID IN: trigger_new]){
    system.debug('##### new formula value'+ q.formula__c);
   c_field = A_field / formula__c;( the formual val points to 700 while in the record it calculated the right value to be 400).
            }

Debug Result :

$ 700

enter image description here

6

You might double check you're doing an after trigger. For before triggers, the trigger context has the new values for the trigger object loaded, but nothing has been committed yet, so any calls to the database will return the old values for both formula fields and regular fields. In after triggers this is not the case.

Imagine the following trigger

trigger Example on Account (before update, after update) {

  system.debug('==>trigger.new[0].name: ' + trigger.new[0].name);
  system.debug('==>trigger.old[0].name: ' + trigger.old[0].name);
  system.debug('==>queried: ' + 
               [select name from Account where id = :trigger.new[0].id][0].name);

}

Then imaging the following code

Account account = new Account(name = 'Hello');
insert account;
account.name = 'World';
update account;

For the update you'd expect the following output

/* Before Trigger */
==>trigger.new[0].name: World
==>trigger.old[0].name: Hello
==>queried: Hello

/* After Trigger */
==>trigger.new[0].name: World
==>trigger.old[0].name: Hello
==>queried: World

Things can get stranger if you something causes a second round of trigger execution, such as a field update. The values are what you'd expect for the first round, but the second round you'd expect trigger.old[0].name to match the queried value in the before trigger, but that's not actually the case.

/* Before Trigger - Pre Workflow */
==>trigger.new[0].name: World
==>trigger.old[0].name: Hello
==>queried: Hello

/* After Trigger - Pre Workflow */
==>trigger.new[0].name: World
==>trigger.old[0].name: Hello
==>queried: World

/* Before Trigger - Post Workflow */
==>trigger.new[0].name: World
==>trigger.old[0].name: Hello
==>queried: World

/* After Trigger - Post Workflow */
==>trigger.new[0].name: World
==>trigger.old[0].name: Hello
==>queried: World
  • To put it another way, you shouldn't query for the records your trigger is acting on, just use the Trigger.Old and Trigger.New maps. – Daniel Hoechst Apr 27 '13 at 3:59
  • A good suggestion, trigger context vars are a lot better than querying and more efficient. That said, the behavior of trigger.old through multiple trigger executions is hardly intuitive. You'd think trigger.old would get reloaded when the 2nd or 3rd round of trigger executions take place, but it doesn't leaving you to manually override it. – Ralph Callaway May 6 '13 at 15:23

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.