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
Colin LoretzColin Loretz 

Apex Rollup Summary Fields

I wrote the following code (replaced object/field names for simplicity) to handle rollup summary fields in Apex. It works pretty well but I frequently get errors when operating on bulk records or if no records exist (out of bounds, etc).

ParentObject__c :has_many Objects__c

Can anyone provide input as to whether or not I'm handling this type of task properly?

Code:
trigger rollupTrigger on Object__c (after insert, after update, after delete) {
    
    double totalSum = 0;
  
    Object__c myObjects = trigger.new;
    
    if(Trigger.isUpdate || Trigger.isInsert)
    {
            for(Object__c object : myObjects) //assume more than 1 record in trigger.new
            {
              for(ParentObject__c parent : [select Id, Name from ParentObject__c where Id = :object.Parent_Object__c]) //get the parent object
              {
                for(Object__c childObject : [select Id, Name, Number__c where Parent_Object__c = :parent.Id]) //get all the objects that belong to the parent object
                {
                  totalSum += childObject.Number__c;   //sum the field of choice
                }
                parent.Total__c = totalSum;   //set the total in the parent
                update parent                       // update the parent
              }
            }
    }
    else if(Trigger.isDelete)
    {
        Object__c [] oldObjects = Trigger.old;
        
      for(Object__c oldObject :oldObjects)    //assume deletion of more than 1 record
      {
        for (ParentObject__c parentObject : [select id, Name, Number__c, Total__c where Id = :oldObject.Parent_Object__c]) //get the parent object(s)
        {
          for (Object__c childObject : [select id, Name, Number__c from Object__c where Parent_Object__c = :parentObject.id])   //get the records that belong to the parent
          {
             totalSum += childObject.Number__c;  //sum the fields after a record has been deleted
          }
            parentObject.Total__c = totalSum;   //set new total in parent
            update parentObject;                      //update parent
        }   
      } //end oldObject for loop
    } //end else if
    
} //end trigger

Thanks!
 

VisualForceVisualForce

Hi..

    I am in same situation... If u have a solution for above code please post it.. Its very helpful for us....

GanuGanu
Hi Colin,
I just look in to your code its similar to my requirement,Can you please post your updated code for an "bulk updates" Or you have any suggestions.
Thanks in advance.
Colin LoretzColin Loretz
I just did a write up on how I implemented Apex rollup summary fields.

http://colinloretz.com/2008/10/salesforce-rollup-summary-fields-using-apex-code/
SFDC-AcumenSFDC-Acumen
Hi Colin, does this address the bulk insert/update issue? To me it looks like there are SOQL queries within for loops - does this not cause any problems?
Colin LoretzColin Loretz
This does not accommodate bulk processing.

Dschach and I are going to be going over the code next week at Dreamforce to hopefully add that functionality.
reidjnreidjn

For those interested in checking it out, there is a free AppExchange app called Rollup Helper that helps to build rollup summaries using lookup relationships or in other cases where the platform simply won't let you to. The app has a UI to configure user defined conditional rollups and can even reference source values on related tables.

 

The free version is available here:
https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI

 

Enjoy!

 

Jerry Reid

Passage Technology