You need to sign in to do that
Don't have an account?
Formula field not evaluating in test class
I have a formula field called 'Status' that assigns a Status to a record based on the value provided in a date field on the same record. Simply put the logic is: If Start Date is not null then Status equals 'Priority'. Otherwise Status equals 'Inactive'.
I have tested this pretty thouroughly in the UI and haven't found any problems.
I am using this object in a VF controller and I need to test both conditions...the records that are a Priority and the records that are Inactive. So my test inserts a bunch of records without a Start Date which means that none of them would be a Priority. I then try to update some of those records with a Start Date to test the other condition. Here is where I am running into trouble. For some reason, the formula is not evaluated. I have verified with system.debug that although there is a Start Date present on the records the Status field is still 'Inactive'.
Does anybody know why the formula field would not fire in a test method?
Formula does get calculated in test method as well, I would request you to just SOQL the formula field before using it like
if you have na formula field on Account where ever you use formula field in test method just query that field before using it.
Let me know if any issues in it.
Thanks for the suggestion. However, I am already calling the field in SOQL. The code below is the section that does it. The formula field is Status__c. The updates that I am making to the Start and End Date fields are suppposed to make the value of the Status__c field change as they do in the UI. After not getting the test coverage I expected I placed a system.debug after the update statement and find that the value of Status__c did not change.
Query this formula field just after the update call and then debug like
This must show you updated formula as the formula value after update will only get calculated when you again fetch this using SOQL.
Did anyone ever find an answer to the formula calculation issue? It seems that querying the record doesn't work. I imagine that the process to calculate formula fields has a low priority and the test method is simply running too fast for that to occur.
Any Solution ?
Little late but answer is below. I've been pulling my hair out over this recently.
http://boards.developerforce.com/t5/General-Development/Test-Coverage-and-Formula-Fields/m-p/289185#M55742