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
HuluSFAdminHuluSFAdmin 

APEX Unit Test and Roll Up Summary fields

I have custom object with Roll Up Summary Fields. In a unit test I create the custom object but the roll up summary fields are always null. Any idea how to set these fields in my unit tests?

 

Thanks

Chaitanya

dmchengdmcheng
 Did you create any child records so that the rollup fields have data to summarize?
HuluSFAdminHuluSFAdmin
I did create the child objects ...
dmchengdmcheng
If you enter the data through the regular Salesforce web interface, do the rollup fields calculate?
HuluSFAdminHuluSFAdmin
Yes just not in the unit tests
CaptainObviousCaptainObvious

Did you insert the child record? :smileyvery-happy:

It's hard to tell without the code/snippet of the unit test...

rfanrfan

I have run into this issue as well.

 

In the unit test, I have a COUNT roll up summary field and the value is always 0 (even after inserting a record that should bump up the value to 1).  During actual running of the code (outside of the test suite) the roll up summary field is computed accurately.

 

Is there a statement that can be run in unit tests to compute the roll up summary fields so that they can be adequately tested in the unit test suite?

FlorentFlorent

Hello,

 

I have the same problem only with a formula field. This formula field allows for an opportunity to "fetch" a value of its related account.

For example, ACC01 has value ACC01.code = 'AA'.

Then if for opportunity OPP01, the account is specified as being ACC01, OPP01.account_code = 'AA'.

 

In my unit tests, however, OPP01.account_code is always null, resulting in a validation rule to stop the "insert OPP01;"…

Of course, I do need that validaiton rule. And of course, ACC01 is inserted BEFORE OPP01.

 

What I would like, is the ability to "set" OPP01.account_code to ACC01.code (or: OPP01.account.code…).

 

Anyone? ^^

maltesmaltes

I have the very same problem.

There is a custom object with roll-up summary field and although I do insert child objects which should set the roll-up summary field in the parent object to a certain amount it always stays null. It works outside the test cases perfectly. Does anyone have an idea? Please!

maltesmaltes

Ok, found a solution which works for me:

http://www.faqoverflow.com/salesforce/5107.html

After inserting the child records I thought it would be enough to update the parent record and then use the same object, but it looks like one has to retrieve the whole record with the roll-up summary fields via a SOQL Query.

Jerry DaruJerry Daru
I am currently having this problem...
Here are the steps I am completing:

1) insert the parent records
2) insert the child records
3) requery the children to verify operations completed
4) requery the parents rollup field

I am still getting no data in my rollups which is causing one of my validations to fail...
Anyone have any ideas what my problem might be?
Tarun Gupta 60Tarun Gupta 60
You might want to check the 'filter criteria' on Roll up field. As if the test record you are creating doesn't match the filter criteria , the roll up will not work.