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

Apex max heap size limits size of attachment one can upload via a VF page

I'm building a Visualforce page that uses the inputFile component to create an attachment.


All works fine. However, the heap size limit prevents me from being able to upload an attachment larger than 3MB, even though Salesforce itself allows attachments of up to 5MB.


Any suggestions on how I might overcome this obstacle?



Turns out the issue had to do with how I was creating my Attachment object.


Originally I was initializing the fields within the constructor (which is how I typically create an SObject). Switching to the default constructor (i.e. no parameters) and setting the field values after solved the problem (???).


Salesforce has acknowledged that there should be no difference between the two and that this is a bug. I have not heard anything further as to whether the issue has been resolved.



// heap size limit exceeded
Attachment att = new Attachment(Body = this.doc.Body, ContentType = this.doc.ContentType, Name = this.doc.Name, ParentId = attAccount.Id);

// works fine 
Attachment att = new Attachment();
att.Body = this.doc.Body;
att.ContentType = this.doc.ContentType;
att.Name = this.doc.Name;
att.ParentId = attAccount.Id;