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
shail.sfdcshail.sfdc 

Formula Field : Order of Execution

I've to write a Simple validation in my Custom VF/Apex code where a formula field (Grand Total which depends on 3 other fields) should not exceed a set number. If it does I would write a condition in my Apex code & add a error message to page. 

My Issue is that I think formula fields are only evaluated once the record is saved & when the page loads again & that's the reason I can't check for the modified Grand Total field in my apex code when User saves a record. 

 

Any workaround this , can this be achieved through After Trigger ?

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Formula fields are evaluated at access time, be it through the UI or the API or in Apex, so you should be alright evaluating it in APEX.

All Answers

chris.noechris.noe

Yes, you should be able to evaluate the formula field within an After trigger.  Also, here is some information on the order of execution for salesforce transactions - http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

Ritesh AswaneyRitesh Aswaney

Formula fields are evaluated at access time, be it through the UI or the API or in Apex, so you should be alright evaluating it in APEX.

This was selected as the best answer
shail.sfdcshail.sfdc

Thanks for the Input friends 

 

I tried validation in a before Trigger & to my surprise( I thought Formula values are only calculated after data is committed) the new calculated formula value was available in the triggr. I wrote a simple Before trigger which is servicng my purpose correctly as below(As far as I've test it). 

 

trigger Trigger_ValidateGrandTotal on Account (before update) {

for( Account acc:trigger.new)
{
if(acc.Grand_Total__c > 50)
{
acc.addError('Limit Exceeded');
break;
}
}
}