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
aKallNVaKallNV 

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?

 

 

Shashikant SharmaShashikant Sharma

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.

aKallNVaKallNV

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.

 

for(DeployedActionItems__c testAI : [select ID, Name, Status__c, StartDate__c from DeployedActionItems__c where DeployedStrategy__c IN: newStrats Limit 10]) {
			testAI.StartDate__c = date.Today()-10;
			testAI.EndDate__c = date.Today()-1;
			testAI.Stage__c = 'Not Started';
			aiUpdate.add(testAI);			
		}
		update aiUpdate;

 

Shashikant SharmaShashikant Sharma

Query this formula field just after the update call and then debug like

 

for(DeployedActionItems__c testAI : [select ID, Name, Status__c, StartDate__c from DeployedActionItems__c where DeployedStrategy__c IN: newStrats Limit 10]) {
			testAI.StartDate__c = date.Today()-10;
			testAI.EndDate__c = date.Today()-1;
			testAI.Stage__c = 'Not Started';
			aiUpdate.add(testAI);			
		}
		update aiUpdate;


for(DeployedActionItems__c testAI : [select Status__c from DeployedActionItems__c where DeployedStrategy__c IN: newStrats Limit 10]) {
			system.debug(' Status Formula Field : ' + testAI.Status__c );			
		}

 This must show you updated formula as the formula value after update will only get calculated when you again fetch this using SOQL.

colemabcolemab

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.

MeetaGulatiMeetaGulati

Any Solution ?

DarrellDDarrellD

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

huskerwendyhuskerwendy
DarrellD the link you provided has a 404 error. I'm experiencing the same problem. Can you provide a new link or tell me what you've done to fix it?
Vj@88Vj@88
Any Solution for this Issue?
Gilbert De LeonGilbert De Leon
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008miOIAQ - here is the link.