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
Srikant Swain 8Srikant Swain 8 

Custom Rollup using batch class for more than 5 Lakh record from 2 child object fields and calculate both at the end populate on parent account field. Any suggestion for optized solution.

Best Answer chosen by Srikant Swain 8
SubratSubrat (Salesforce Developers) 
Hello Srikant ,

When dealing with a large number of records and performing custom rollup calculations in Salesforce, it's essential to optimize the solution to ensure efficient processing. Here's a suggested optimized solution using a Batch Apex class to perform the custom rollup on more than 5 Lakh records:

Batch Apex Class:
Create a Batch Apex class to perform the rollup calculations. The batch class allows you to process records in smaller chunks, which is more efficient for large data volumes.
public class CustomRollupBatch implements Database.Batchable<SObject> {

    public Database.QueryLocator start(Database.BatchableContext context) {
        // Query the child records with necessary fields for rollup calculation
        return Database.getQueryLocator('SELECT Field1_c, Field2c, Parent_Accountc FROM Child_Object_c');
    }

    public void execute(Database.BatchableContext context, List<SObject> scope) {
        // Map to store rollup results for each parent account
        Map<Id, Decimal> parentRollupMap = new Map<Id, Decimal>();

        for (SObject record : scope) {
            // Perform rollup calculation for each record
            // Replace 'Field1_c' and 'Field2_c' with your actual fields for rollup
            Decimal rollupValue = (Decimal)record.get('Field1_c') + (Decimal)record.get('Field2_c');
            
            Id parentId = (Id)record.get('Parent_Account__c');
            if (parentRollupMap.containsKey(parentId)) {
                parentRollupMap.put(parentId, parentRollupMap.get(parentId) + rollupValue);
            } else {
                parentRollupMap.put(parentId, rollupValue);
            }
        }

        // Update parent account records with the rollup result
        List<Account> parentUpdates = new List<Account>();
        for (Id parentId : parentRollupMap.keySet()) {
            Account parentAccount = new Account(Id = parentId);
            parentAccount.Custom_Rollup_Field__c = parentRollupMap.get(parentId);
            parentUpdates.add(parentAccount);
        }
        update parentUpdates;
    }

    public void finish(Database.BatchableContext context) {
        // Optionally, you can perform any final cleanup or notification logic here.
    }
}
Scheduling the Batch Job:
To schedule the batch job, navigate to "Apex Classes" in Salesforce Setup and click "Schedule Apex." Choose the "CustomRollupBatch" class, set the preferred frequency (e.g., daily or weekly), and specify the start time for the job.

By using this Batch Apex approach, the rollup calculations will be performed efficiently in smaller chunks, which can handle large data volumes without hitting governor limits. The code should be customized to fit your specific child object fields and parent account field for rollup. Additionally, ensure that the field types and data volume align with the appropriate data types to avoid any data-related issues.

If this helps , please mark this as Best Answer.
Thank you.

All Answers

Sukanya BanekarSukanya Banekar
Hi Srikant,

If you only require the roll-up field for viewing purposes, on the layout, you can achieve this by using a summary report. You can add the report to the page layout or Lightning page for easy access.

However, if you need a custom roll-up field to be usable in queries or bulk reads, you can implement a queuable solution:
1. Create a new object, such as 'Queue Record.'
2. Whenever there is a change in the child record values, create a Queue record and execute a queuable process to roll up the required values.

By implementing this approach, the roll-up values will be accurately updated, enabling you to use them in queries or perform bulk read operations.

Please let me know if this helps.

Thanks,
Sukanya Banekar
SubratSubrat (Salesforce Developers) 
Hello Srikant ,

When dealing with a large number of records and performing custom rollup calculations in Salesforce, it's essential to optimize the solution to ensure efficient processing. Here's a suggested optimized solution using a Batch Apex class to perform the custom rollup on more than 5 Lakh records:

Batch Apex Class:
Create a Batch Apex class to perform the rollup calculations. The batch class allows you to process records in smaller chunks, which is more efficient for large data volumes.
public class CustomRollupBatch implements Database.Batchable<SObject> {

    public Database.QueryLocator start(Database.BatchableContext context) {
        // Query the child records with necessary fields for rollup calculation
        return Database.getQueryLocator('SELECT Field1_c, Field2c, Parent_Accountc FROM Child_Object_c');
    }

    public void execute(Database.BatchableContext context, List<SObject> scope) {
        // Map to store rollup results for each parent account
        Map<Id, Decimal> parentRollupMap = new Map<Id, Decimal>();

        for (SObject record : scope) {
            // Perform rollup calculation for each record
            // Replace 'Field1_c' and 'Field2_c' with your actual fields for rollup
            Decimal rollupValue = (Decimal)record.get('Field1_c') + (Decimal)record.get('Field2_c');
            
            Id parentId = (Id)record.get('Parent_Account__c');
            if (parentRollupMap.containsKey(parentId)) {
                parentRollupMap.put(parentId, parentRollupMap.get(parentId) + rollupValue);
            } else {
                parentRollupMap.put(parentId, rollupValue);
            }
        }

        // Update parent account records with the rollup result
        List<Account> parentUpdates = new List<Account>();
        for (Id parentId : parentRollupMap.keySet()) {
            Account parentAccount = new Account(Id = parentId);
            parentAccount.Custom_Rollup_Field__c = parentRollupMap.get(parentId);
            parentUpdates.add(parentAccount);
        }
        update parentUpdates;
    }

    public void finish(Database.BatchableContext context) {
        // Optionally, you can perform any final cleanup or notification logic here.
    }
}
Scheduling the Batch Job:
To schedule the batch job, navigate to "Apex Classes" in Salesforce Setup and click "Schedule Apex." Choose the "CustomRollupBatch" class, set the preferred frequency (e.g., daily or weekly), and specify the start time for the job.

By using this Batch Apex approach, the rollup calculations will be performed efficiently in smaller chunks, which can handle large data volumes without hitting governor limits. The code should be customized to fit your specific child object fields and parent account field for rollup. Additionally, ensure that the field types and data volume align with the appropriate data types to avoid any data-related issues.

If this helps , please mark this as Best Answer.
Thank you.
This was selected as the best answer