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
stcforcestcforce 

Heap problems setFileAtt​achments() of outbound email - please help (trying again)

Hello all. I'm creating a controller to send an email but the heap size is 6mb and the act of creating attachments has the effect of creating duplicates (however briefly) of the list of attachments, which would suggest that for a synchronous execution, you can at most have 3mb (total) of attachments. This seems impossibly restrictive. Does anyone have any ideas (other than asynchronous apex) to address this? The requirement is to produce a custom email send functionality in an image heavy org. Consequently, they want to be able to send the maximum size of attachments. The question becomes: does this deep copy behavior represent an absolute limit or is there a potential workaround?

I would appreciate any help anyone is able to give.

thank you.

 

code/debug results to illustrate the problem:

 

Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();

Messaging.EmailFileAttachment[] someAttach= newlist<Messaging.EmailFileAttachment>();

attachment a = [Select a.Name, a.ContentType,a.Body FromAttachment a limit 1];

Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();

attach.setFileName(a.Name);

attach.setInline(false);

attach.Body=a.body;

attach.setContentType(a.ContentType);

someAttach.add(attach);

a = null;

System.debug('Limit pre: '+Limits.getHeapSize()+' out of '+Limits.getLimitHeapSize()+'.\n ');

if(!someattach.isEmpty()) mail.setFileAttachments(someAttach);

System.debug('xxx Limit post: '+Limits.getHeapSize()+' out of '+Limits.getLimitHeapSize()+'.\n ');

 

the above code produces:

 

09:51:51.686 (3686665000)|USER_DEBUG|[79]|DEBUG|Limit pre: 16761 out of 6000000.

09:51:51.687 (3687050000)|USER_DEBUG|[81]|DEBUG|xxx Limit post: 32386 out of 6000000.

 

which would seem to be a deep copy of the entire list and consequently presents problems for the heap limit.

path2cloudpath2cloud

Not sure of the exact issue, but if you want to avoid hitting the heap size 6MB limit, try using transient keyword while declarign the Blob variable and use it for Body of the atatchment

 

 

Transient Blob body {get; setl}

 

<apex:inputfile value="{!body}">

 

Attachment a = new Attachment();

a.body = body;

 

 

stcforcestcforce

Transient won't help. the issue is that the method to add attachments performs a complete, deep copy of the attachment list. If you have 3mb of attachments, then it seems that you will immediately use 6mb of the heap.