Find your content:

Search form

You are here

Are rollup fields accessible in test methods?


When I create an object and some associated child objects in a test method, the rollup fields on the parent are always null. Is there some common approach to handling this?

I think the same issue is described here:

I'll work on a quick code example, but just wanted to get this out there in case it was an easy/common question.


Attribution to: Seth Stone

Possible Suggestion/Solution #1

I have a Roll-Up Summary field on Opportunity that gives the SUM of a custom Opportunity Product (OpportunityLineItem) field.

In a test case the Roll-Up Summary field was populated when I did an explicit SOQL query to pull back the new calculated value.


Opportunity opp = new Opportunity();
// Set various Opp fields
insert opp;

OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = opp.Id;
// Set various OLI fields.
oli.FieldToRollUp__c = 1000.00;
insert oli;

// Query the Opportunity again to get the updated rollup summary field.
Opportunity oppAfterInsert = [Select RollUpSummaryField__c from Opportunity where Id= :opp.Id];
System.assertEquals(1000.00, oppAfterInsert.RollUpSummaryField__c);

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #2

It sounds to me like you're not re-querying the parent for testing (please correct me if I'm wrong). The steps to get this working would be like so:

  1. insert your parent record.
  2. insert you child record/s.
  3. query the parent record.
  4. tests should contain the rollup values.

Attribution to: Daniel Blackhall
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content