Apex – Issue While Uploading File Greater Than 5MB

I am trying to upload a file in salesforce and send it to Sharepoint for storage using synchronous call.
However, when i upload file more than 5MB i am encountering an issue like below :'
{FAILURE=Exceeded max size limit of 6000000 with request size 6004736}

The debug logs says the following :
enter image description here

Limits.getHeapSize——–10766690

Limits.getLimitHeapSize——-6000000

I am aware that the heap size limit is 6MB. But earlier when i was uploading 24.5MB it was uploading and storing in Sharepoint without any issue. Recently in last 2 weeks i am seeing this error of Max size for even 6MB files.

After searching in Google, i found that making variables as transient or using @future solves issue, but i can use neither in my code.
Posting the code for your reference :

 public static HttpRequest prepareRequestWithBlob(String endPointURL, Attachment file, String method){

    String fileByteSize = String.valueOf(file.BodyLength-1)+'/'+String.valueOf(file.BodyLength);
    system.debug('fileByteSize --------'+fileByteSize);
    system.debug('Limits.getHeapSize--------'+Limits.getHeapSize());
    system.debug('Limits.getLimitHeapSize-------'+Limits.getLimitHeapSize());
    HttpRequest request = new HttpRequest();
    //if (Limits.getHeapSize() < Limits.getLimitHeapSize()){
    request.setMethod(method);
    request.setHeader('Content-Type','application/json');
    request.setHeader('Accept','application/json;odata.metadata=full');
    request.setHeader('Content-Length', String.valueOf(file.BodyLength));
    request.setHeader('Content-Range', 'bytes 0-'+fileByteSize);
    system.debug('request in SPU---------'+request);          
    string fileEncoded = EncodingUtil.base64Encode(file.Body);

    Blob bodyBlob = EncodingUtil.base64Decode(fileEncoded);

    request.setBodyAsBlob(bodyBlob);
    request.setTimeout(120000);
    request.setEndpoint(endPointURL);
  //  }else{
  //      system.debug('Throw custom exception');
  //  }

    return request;
} 

The line string fileEncoded = EncodingUtil.base64Encode(file.Body); in the above code fails everytime i upload file > 5MB.

Your help is greatly appreciated on how to handle this type of error.

Best Answer

The actual limit is going to be closer to 4,500,000 bytes before encoding. Note that it has never been possible to send more than 12,000,000 bytes of data in a single transaction, and even then, only for asynchronous code. If for some reason you were able to send more than 6MB/12MB, this was a bug that has since been fixed.

It should never have been possible to send more than 12MB payloads under any circumstances. The net result is that you won't be able to do this in Apex Code. Instead, you'll want to use the AJAX Proxy to send the data directly from the user's browser to its final destination. Unlike Apex Code, it has theoretically unlimited payload sizes (I've tested this with files exceeding 250MB).

Governor limits are not meant to be broken, and there is no workaround. I don't know the specifics of the SharePoint API, but you might be able to break up your request into multiple parts if you still want to use Apex Code, but it still would not be a single-transaction, synchronous callout.

Related Topic