You need to sign in to do that
Don't have an account?
Florent
Update formula field in unit test
Hi all,
I have a custom formula field in the Opportunity object, which takes value from a field in the Account objet. This value is then checked in a validation rule.
But in my unit test, this validation rule always replies FALSE because the formula field value is "null"…
How can I update this formula field within my test class so that the trigger I'm trying to test actually runs ?
See here for the complete version of my question (watch out, it's very long, and you're most likely to be scared away! ,-)
The answer to my problem can be found here, in case someone encounters a similar issue:
http://boards.developerforce.com/t5/Apex-Code-Development/How-to-update-a-required-formula-field-to-null-before-very-first/m-p/454473/highlight/false#M82841
In short, I need to assign account ID t the opportunity opp01 like this: opp01.AccountId = acc01.id, and not like I did: opp01.Account = acc01, which I assumed would allow me to access all of account acc01 values -- when in fact, no it doesn't.
All Answers
Hi Florent,
Formula field gets set after the record is saved in database. However, the Validation rule gets called before the record is saved. This is the standard flow of execution in salesforce. Your validation will always FAIL since you are reading value form a formula field.
Possible Workaround:
You can modify your validation rule to read the field on parent account object rather than reading a formula field.
So the criteria of validation rule will be something like :
Account__r.<field name>
Thanks,
Pragati
Hi Pragati,
Thanks a lot for your answer. I did read the Triggers and Order of Execution, but hadn't understood "workflow field updates" (point 10.) are the same as (i suppose) as formula fields…
So I tried your workaround, but it doesn't work.
Log file says (user debug are done before insert). :
But then, validation rule fails :
Which I really don't understand…
The answer to my problem can be found here, in case someone encounters a similar issue:
http://boards.developerforce.com/t5/Apex-Code-Development/How-to-update-a-required-formula-field-to-null-before-very-first/m-p/454473/highlight/false#M82841
In short, I need to assign account ID t the opportunity opp01 like this: opp01.AccountId = acc01.id, and not like I did: opp01.Account = acc01, which I assumed would allow me to access all of account acc01 values -- when in fact, no it doesn't.