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
Nagisetti Satya rameshNagisetti Satya ramesh 

How to "deep clone including Attachments" without hitting the heap space governor limit?

heap size
SwethaSwetha (Salesforce Developers) 
HI Satya,

Some best practices are
> First query all the attachments.
>Count the number/size of the attachments and if there are less than say 50 and all are 2M or less then query and update one at a time.
>If there are more than 50 attachments or they are larger than 2M, use batch processing to update the attachments.
> Use the Limits methods to check your code's heap size and avoid exceeding the limit.

Recommend reviewing 
https://salesforce.stackexchange.com/questions/34443/how-to-deep-clone-including-attachments-without-hitting-the-heap-space-governo
https://developer.salesforce.com/forums/?id=9062I000000IPBmQAO

If this information helps, please mark the answer as best. Thank you
Arun Kumar 1141Arun Kumar 1141
Hi Nagestti,

To perform a deep clone including attachments without hitting the heap space governor limit, you can follow these strategies:

Use Batch Processing: Divide the deep cloning process into smaller batches of records. Process a limited number of records at a time, clone them, and then move on to the next batch. This can help keep the heap size within limits for each batch. Make use of batch Apex or recursion with smaller subsets of records.

Asynchronous Processing: Use asynchronous processing, such as Queueable Apex or future methods, to perform the deep cloning. This allows you to break up the operation into smaller chunks and run them asynchronously, which can help avoid heap size limits.

You can also refer from the below site:
https://salesforce.stackexchange.com/questions/34443/how-to-deep-clone-including-attachments-without-hitting-the-heap-space-governo

Hope this helps you.
Thanks!