Skip to main content

I am trying to get AggregateResult

my query is like

 List<AggregateResult> outpatientYears = [

      SELECT CALENDAR_YEAR(Date_of_Service__c) Year

      FROM Case

      WHERE Case_Type__c = 'The Type'

      GROUP BY CALENDAR_YEAR(Date_of_Service__c)

      ORDER BY CALENDAR_YEAR(Date_of_Service__c)

    ]; 

 

working for less then fifty thousand,

I have to make it work for 1 Million Records

 

@* Salesforce Developers * 

5 réponses
  1. 19 sept. 2024, 06:16

    @shubha

    To overcome this limit in an @AuraEnabled method or any Apex method, you need to adjust your approach rather than increasing the limit. Here are a few strategies you can consider:

    1. Pagination with SOQL Queries:

    Instead of querying all records at once, implement a pagination approach where you query a limited number of records in batches. Use the OFFSET keyword to query the next set of records.

    public with sharing class PaginatedQueryExample {

    @AuraEnabled

    public static List<Account> getAccounts(Integer offset, Integer limitSize) {

    return [SELECT Id, Name FROM Account LIMIT :limitSize OFFSET :offset];

    }

    }

    In this example, you fetch a limited number of records each time and provide the next offset for subsequent calls.

    2. Use Custom Apex Iterable (Batch Processing):

    If you need to process a large volume of records, use Batch Apex. This allows you to process records in chunks, bypassing the governor limit:

    public class BatchAccountProcessing implements Database.Batchable<SObject> {

    public Database.QueryLocator start(Database.BatchableContext BC) {

    return Database.getQueryLocator([SELECT Id, Name FROM Account]);

    }

    public void execute(Database.BatchableContext BC, List<SObject> scope) {

    // Process your batch of records here

    }

    public void finish(Database.BatchableContext BC) {

    // Post-processing

    }

    }

    Trigger the batch job when needed.

    3. Using a SOQL Query Locator (For Large Query Processing):

    If you're processing a large query and don't need to display all results at once, use the Database.QueryLocator. This allows you to handle large queries efficiently in a batch context.

    4. Utilize Apex Continuations (Asynchronous Requests):

    If you're working with an external API or process, consider using Continuations in Lightning Components, which enable asynchronous requests without hitting the synchronous limits.

    5. Selective Queries with Filters:

    Reduce the number of records returned by refining your query. Use selective filtering (e.g., indexed fields) to query only the necessary data. For example:

    List<Account> filteredAccounts = [SELECT Id, Name FROM Account WHERE CreatedDate = LAST_N_DAYS:30];

    6. Consider Async Apex (Future/Queueable Jobs):

    When performing complex or long-running operations, offload the processing to a Future or Queueable job. This allows the operations to run asynchronously and avoids hitting synchronous limits in a single execution.

0/9000