We are using this APEX REST API class to take pdf from our third party client. Functionality works fine however when file size is more than 6 MB, we get an error:

Apex heap size too large: 8014366

I understand we are hitting heap here however, is there a way that we can bypass this error? It seems weird to me that Salesforce doesn't allow sending more than 6 MB document on APEX REST API. Is there a possible workaround to this issue?


global with sharing  class SampleV1 {

    global static CaseWrapper doPost(String Title, String JSON, String VersionData, string Description, string PathOnClient, String ClientId  ) {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        CaseWrapper response = new CaseWrapper ();

            List <ContentVersion> CaseListInsert = New List <ContentVersion> ();  

            ContentVersion CV = new ContentVersion ();
            CV.Title = Title ;
           // CV.JSON__c= Jsonsample;
            CV.VersionData = EncodingUtil.base64Decode(VersionData);
            CV.Description = 'Sample';                                 
            CV.PathOnClient = 'Sample Doc.pdf';            
            CV.ClientId__c = ClientId; 

            Insert CV;

        response.status = 'Success';
        response.StatusCode = 201;

        response.message = 'Sample PDF was created successfully.';
        return response;

    global class CaseWrapper {
        public List<Attachment> AttachmentList;
        public String status;
        public String message;
        public integer StatusCode;

        public CaseWrapper (){
            AttachmentList = new List<Attachment>();
  • 2
    How often do you think you'll encounter this limit? 6 MB for a pdf file is pretty generous. At 451 pages long, NASA's FY 2007 Budget Request is just over 5 MB (no idea why I have that downloaded, but I do). That begs the question, what exactly is included in these PDF files you're receiving? Instead of trying to work around that limit, I would suggest first looking at decreasing the size of your pdf (reduce/compress images, compess/zip the pdf), or taking only the required information and generating the PDF using a Visualforce page.
    – Derek F
    Commented Sep 25, 2017 at 14:55

1 Answer 1


The heap size is limited to 6mb when running a synchronous Apex code, but can be increased to 12mb if the call is asynchronous (see Per-Transaction Apex Limits).

What is important to note here is that the heap limit applies to Apex, but not to the file size. You can, for example, upload a file to the platform up to 37mb in size, according to the documentation:

You can insert or update blob data using a non-multipart message, but if you do, you are limited to 50 MB of text data or 37.5 MB of base64–encoded data.

But this is valid when you are using the standard REST API, not a custom endpoint, like you are trying to do.

The workaround would be to use your endpoint to upload the basic data you need (less than 6mb), and then, if Salesforce replies that the record was created successfully, upload your file as an attachment to your Salesforce record. That is, if your data exceeds the heap size.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .