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
imrohitimrohit 

Issue in batch class to generate a excel sheet

I have created a batch class to generate excel file of salesforce records, data to generate in excel is aroud 2-3 lakhs but every time batch class fails with CPU time limit exceed or Apex Heap size issue.

Please help if there is another way to achieve this.

SwethaSwetha (Salesforce Developers) 
HI Rohit,

> Instead of processing all the records at once, you can divide them into smaller chunks and process them iteratively. For example, you can retrieve and process 10,000 records at a time and then generate the Excel file. Repeat this process until you have processed all the records.

> You can leverage Queueable Apex to offload the processing to the background, allowing for more CPU time and heap space. In this approach, you enqueue a job for each chunk of records, and the job processes the records and generates the Excel file. Once a job is completed, it can enqueue the next job.

>If real-time processing is not a requirement, you can consider using the Salesforce Streaming API. With this approach, you can create a trigger or process builder that listens for changes on the Salesforce records you are interested in. When a record is updated or created, you can capture the changes and accumulate them in memory. Once you have accumulated a sufficient number of changes, you can generate the Excel file.

Related:https://salesforce.stackexchange.com/questions/269543/not-able-to-add-more-than-2000-records-in-excel-file-using-batch-class

Some steps you can take to address the "Apex CPU time limit exceeded" issue:
- Minimize SOQL queries and DML operations: Reduce the number of database operations by combining queries and updates where possible.
- Nested loops can quickly consume CPU time, especially if you have large data sets. Try to find alternative ways to achieve the same result without using nested loops.
- Utilize collections like lists, sets, and maps to process data in bulk rather than iterating over individual records.
- If your code is processing a large amount of data, consider filtering or reducing the data set to only include the necessary records. Use selective queries and leverage database indexes to improve query performance.
- To avoid hitting CPU limits, you can offload CPU-intensive operations to asynchronous processing methods such as Batch Apex, Queueable Apex, or Future methods. These methods execute in the background and have higher CPU limits compared to synchronous transactions.
- If you're performing queries that return a large number of records, consider implementing pagination to retrieve and process records in smaller chunks. This approach allows you to work with a subset of data at a time, reducing CPU usage.
- Enable debug logs and analyze them to identify areas where your code is consuming excessive CPU time. Look for loops, queries, or operations that are taking longer than expected and optimize those sections.

Related: https://www.apexhours.com/apex-cpu-time-limit-exceeded/
SwethaSwetha (Salesforce Developers) 
Important Update - last chance
User-added image
Heads up — this is your last chance to get your Trailblazer account set up and connected to your forums discussions on this site.

Please take these steps before November 30, 2023, 11:59 p.m. PT to ensure your contributions follow you to the Trailblazer Community:
  1. If you’re not already a member of the Trailblazer Community, sign up for a Trailblazer account using the same login email address associated with your Developer Discussion Forums account. This is crucial.
  2. If you already have a Trailblazer account, and it’s using a different email address from your Developer Discussion Forums account, we recommend that you log in to the Trailblazer Community and connect your forums email address to your Trailblazer account.
Find more info & support
We know that moving platforms can be a hassle, so we created a special forums users group, the Migration Support Hub, in the Trailblazer Community where you can find other forums users and get training videos, the latest information, and assistance.

We want to thank you for all of your time, energy, and contributions made here in the Salesforce Discussion Forums.
We’ll see you in the Trailblazer Community!

Sincerely,
The Forums Support Team