How can I handle and optimize the processing of a large list on apex?

255    Asked by ranjan_6399 in Salesforce , Asked on Mar 28, 2024

 I am a Salesforce developer and I am currently working on a custom apex code that can process a large list of records. However, during the time of testing, I encountered an issue related to the size of the list being processed. How can I handle and optimize the processing of large lists in Apex to improve performance and avoid governor limits? 

Answered by Ranjana Admin

 In the context of Salesforce, during the time of dealing with a large list in apex code with Salesforce, it is important to optimize the processing to avoid hitting governor limits. Here are the steps given:-

Using iterable and query locator for large data sets

You can consider using iterable and query locator interfaces for effective data retrieval and processing.

Batch apex for bulk data processing

You can use the batch apex to process large volumes of records asynchronously in smaller batches.

Optimization of query and data processing logic

Try to ensure that your SOQL queries are selective and efficient, fetching only the appropriate fields and records.

Avoiding nested loops and complex logic

You should refactor your code for the purpose of avoiding nested loops and complex logic within loops.

Monitoring and analysis of execution limits

You can use the debug logs of the Salesforce, system. debug statement for the purpose of monitoring and analyzing the performance of coding and governor limit usage.

Here is the example given of how you can optimize the processing of large lists in apex by using iterable, batch apex:-

Public class LargeDataProcessor {
    // Method to process large data using Iterable
    Public void processLargeData() {
        Iterable accounts = new Iterable([SELECT Id, Name FROM Account]);
        For (Account acc : accounts) {
            // Process each account record
            System.debug(‘Processing Account: ‘ + acc.Name);
        }
    }
    // Batch Apex class for bulk processing
    Global class MyBatchClass implements Database.Batchable {
        Global Database.QueryLocator start(Database.BatchableContext BC) {
            Return Database.getQueryLocator(‘SELECT Id, Name FROM Account’);
        }
        Global void execute(Database.BatchableContext BC, List scope) {
            // Process each batch of accounts
            For (Account acc : scope) {
                // Process each account record
                System.debug(‘Batch Processing Account: ‘ + acc.Name);
            }
        }
        Global void finish(Database.BatchableContext BC) {
            // Final processing after all batches are completed
            System.debug(‘Batch Processing Completed’);
        }
    }
    // Method to process large data using optimized SOQL query
    Public void processLargeDataOptimized() {
        List accounts = [SELECT Id, Name FROM Account WHERE CreatedDate >= LAST_N_DAYS:30 LIMIT 1000];
        For (Account acc : accounts) {
            // Process each account record
            System.debug(‘Processing Optimized Account: ‘ + acc.Name);
        }
    }
    // Method to process large data using maps for efficient processing
    Public void processLargeDataWithMap() {
        Map accountMap = new Map([SELECT Id, Name FROM Account]);
        For (Id accountId : accountMap.keySet()) {
            Account acc = accountMap.get(accountId);
            // Process each account record            System.debug(‘Processing Account with Map: ‘ + acc.Name);
        }
    }
}

Your Answer

Interviews

Parent Categories