You need to sign in to do that
Don't have an account?
shail.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 ?
Thanks for the quick response Bob.
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). But I think there has to be a catch, do you see any issue with below code.
trigger Trigger_ValidateGrandTotal on Account (before update) {
for( Account acc:trigger.new)
{
if(acc.Grand_Total__c > 50)
{
acc.addError('Limit Exceeded');
break;
}
}
}
All Answers
Hi,
The formula field is calculated after the record save.So you can't you formula field
In the trigger calculate the calculate the value and validate it.
The after trigger is the right place to be using it IMHO, but you'll need to execute some SOQL that pulls copies of all of the trigger records from the database including the formula field.
I wouldn't recommend replicating the formula in the trigger - that way if you need to change the formula at any point in the future, you have to change two copies, which goes against DRY.
Here is a trigger that does something similar to what you are looking for
There is a Project object that has two fields a) Budget amount and b) Total Payments amount and there is another object called Payment object that when you create or edit rolls up the Amount into Total Payments field. When adding the Amounts the system has to ensure that the Total Payments Amount does not exceed Budget amount
In this trigger you have a SOQL query embedded inside a for loop:
This means that as soon as your trigger processes more than 100 records it is guaranteed to break the SOQL query governor limit I'm afraid.
Hi shail,
we cannot go for any work arounf,bcoz we all know that flow excution is from before trigger,validation and after trigger ,then workflow.So I will suggest you to remove the formula fileds from custom object and create a simple number/text field and go for
calculation(as per your requirment it will calculate and find the grandtotal) thrugh apex and check it before inserting to the object .
Regards,
Itswas.
Thanks for the quick response Bob.
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). But I think there has to be a catch, do you see any issue with below code.
trigger Trigger_ValidateGrandTotal on Account (before update) {
for( Account acc:trigger.new)
{
if(acc.Grand_Total__c > 50)
{
acc.addError('Limit Exceeded');
break;
}
}
}
Thanks for your Input friends, I was able to make i work using the trigger I posted before.
Appreciate your help