I have a question on how to test a formula field from an APEX code coverage script. This is the scenario. There is a custom formula field, Activity_Total_Duration_Time__c, that it is part of my Activity - Event page layout. All what this formula does is to calculate the time between the StartDateTime and EndDateTime from the Event page layout and retrieve that value in minutes. This is the formula: (DurationInMinutes / 60) Very simple formula that works perfect for what I want to accomplish.
The issue I have is that I'm writing the APEX code to test the class in which I use this formula, I'm up to 86% but this part is not being tested and I don't know why.
To set up my test I created an Event in that code and used DateTime to give the necessary values for the formula to fire. Here is a snapshot of the code:
`//Event creation
DateTime dt = System.now();
Event eventTest = new Event();
eventTest.OwnerId='someId';
eventTest.StartDateTime = dt.addMinutes(-120);
eventTest.EndDateTime = dt;
eventTest.Status_Event__c= 'Planned';
eventTest.Summary_Assessment_of_Activity__c = 'Good';
insert eventTest;`
The idea is to give two hours, (120 minutes), for the formula to run, and receive a value back to the Activity_Total_Duration_Time__c. I can't add a initial value from APEX because it is a formula field and it is not writable.
I used the developer console to run this test and read this:
`system.debug(eventTest.StartDateTime);
system.debug(eventTest.EndDateTime);
system.debug(eventTest.Activity_Total_Duration_Time__c);`
The outcome is this:
USER_DEBUG [30]|DEBUG|2014-06-06 04:31:08 <--this looks fine, Stat Time
USER_DEBUG [31]|DEBUG|2014-06-06 06:31:08 <--this looks fine too, End Time
USER_DEBUG [32]|DEBUG|null <-- this one I don't understand.
I run these system.debugs right after inserting the eventTest Event.
Anyone knows why even after inserting values to the fields that the formula needs to make further calculations the return values still 'null'?