Rollup Summary Operations with Custom Code

July 14, 2023


 

 

After completing this unit, you will able to:

1.  Introduction

2.  Understanding Rollup Summary Fields

3.  Apex Triggers and Rollup Logic

 

1.  Introduction

Salesforce is a powerful CRM platform that empowers businesses to streamline their operations, improve customer relationships, and drive growth. One of the essential features of Salesforce is Rollup Summary fields, which provide aggregated data from child records to parent records. While Salesforce offers several standard Rollup Summary operations, there are times when you need custom code to achieve more complex calculations and insights. In this blog post, we will explore how to leverage custom code to enhance Rollup Summary operations in Salesforce.

2.  Understanding Rollup Summary Fields

Rollup Summary fields allow you to calculate and display aggregated data on a parent record based on the values of related child records. Salesforce provides standard Rollup Summary operations like sum, count, min, max, and more. These operations are straightforward to configure using the point-and-click interface. However, there are scenarios where you need advanced calculations or data manipulations that go beyond the standard options.

To perform complex calculations or apply business logic during Rollup Summary operations, you can write custom code using Apex, Salesforce's proprietary programming language. Custom Rollup Summary operations allow you to extend the functionality of Rollup Summary fields to meet specific requirements. With custom code, you have more flexibility to manipulate data, perform calculations, apply filters, and handle any special cases.

3.  Apex Triggers and Rollup Logic

By leveraging Apex triggers and writing custom Rollup Summary operations, you can unlock additional insights, meet specific business requirements, and optimize your data management processes in Salesforce. With careful design, testing, and maintenance, you can harness the full potential of Rollup Summary operations with custom code and drive greater value from your Salesforce implementation.

The code snippet for the rollup summary logic. (Please replace the parent and child object according to your salesforce's parent and child object in the code.)

The logic for the apex trigger:

trigger ChildObjectTrigger on Child_Object__c (after insert, after update, after delete)
{
    Set<String> parentObjectIds = new Set<String>();
    if(trigger.isAfter)
    {
        if(trigger.isInsert)
        {
            for(Child_Object__c ch: trigger.New)
            {
                parentObjectIds.add(ch.Parent_Object__c);
            }
           
            if(parentObjectIds != null && !parentObjectIds.isEmpty())
            {
               
                ChildObjectTriggerHelper.doOperation(parentObjectIds);
            }
        }
       
        if(trigger.isUpdate)
        {
            for(Child_Object__c ch: trigger.Old)
            {
                parentObjectIds.add(ch.Parent_Object__c);
            }
           
            for(Child_Object__c ch: trigger.New)
            {
                parentObjectIds.add(ch.Parent_Object__c);
            }
           
            if(parentObjectIds != null && !parentObjectIds.isEmpty())
            {
                ChildObjectTriggerHelper.doOperation(parentObjectIds);
            }
        }
       
        if(trigger.isDelete)
        {
            for(Child_Object__c ch: trigger.Old)
            {
                parentObjectIds.add(ch.Parent_Object__c);
            }
           
            if(parentObjectIds != null && !parentObjectIds.isEmpty())
            {
                ChildObjectTriggerHelper.doOperation(parentObjectIds);
            }
        }
    }
}
 

The logic for the apex class:

public class ChildObjectTriggerHelper {
    public static void doOperation(Set<String> parentObjectIds)
    {
        List<Parent_Object__c> toBeUpdatedParent = new List<Parent_Object__c>();
       
        if(parentObjectIds != null && !parentObjectIds.isEmpty())
        {
            for(Parent_Object__c parent: [Select Id, Total_Price__c, Total_Child_Record__c, (Select Id, Price__c from Child_Objects__r) from Parent_Object__c where Id IN: parentObjectIds])
            {
                Decimal totalPrice = 0;
                Decimal maxPrice = 0;
                Decimal minPrice = 0;
               
                if(parent.Child_Objects__r.size() == 1 && parent.Child_Objects__r[0].Price__c != null)
                {
                    maxPrice = parent.Child_Objects__r[0].Price__c;
                    minPrice = parent.Child_Objects__r[0].Price__c;
                    totalPrice = parent.Child_Objects__r[0].Price__c;
                }

                if(parent.Child_Objects__r.size() > 1 && parent.Child_Objects__r[0].Price__c != null)
                {
                    maxPrice = parent.Child_Objects__r[0].Price__c;
                    minPrice = parent.Child_Objects__r[0].Price__c;
                    for(Child_Object__c child: parent.Child_Objects__r)
                    {
                        if(child.Price__c != null)
                        {
                            totalPrice += child.Price__c;
                            if (child.Price__c > maxPrice)
                            {
                                maxPrice = child.Price__c;
                            }
                            if (child.Price__c < minPrice)
                            {
                                minPrice = child.Price__c;
                            }
                        }
                    }
                }
               
                parent.Total_Price__c = totalPrice;
                parent.Maximum_Price__c = maxPrice;
                parent.Minimum_Price__c = minPrice;
                parent.Total_Child_Record__c = parent.Child_Objects__r.size();
                toBeUpdatedParent.add(parent);
            }
        }
       
        if(toBeUpdatedParent != null && !toBeUpdatedParent.isEmpty())
        {
            update toBeUpdatedParent;
        }
    }
}
 

So, don't limit yourself to the standard options—dive into the realm of custom code and supercharge your Rollup Summary operations in Salesforce.

 

I hope this blog helped you!