String length exceeds maximum: 6000000

Are there any work arounds for this issue?

I’m trying to upload a file(attachment) to an 3rd party REST API Service. It works fine until my file sizes get about ~5 mb. Then I start getting the following error:

common.apex.runtime.impl.ExecutionException: String length exceeds
maximum: 6000000

The code that I’m using is a little hacky due to the requirements of the external service (must be multipart, does not accept base64).

Pretty much it takes the contents of the attachment blob, and combines them with the multipart header then puts them back into a blob.

Here’s the code I’m using. There are some statics that I left out but you get the point.

public static void UploadAttachment(Attachment attachment, String folder){

    String boundary = '----------------------------741e90d31eff';
    String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="''";\nContent-Type: application/octet-stream';
    String footer = '\r\n--'+boundary+'--';              
    String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
        header+=' ';
        headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
    String bodyEncoded = EncodingUtil.base64Encode(attachment.body);
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));

    Blob bodyBlob = null;
    String last4Bytes = bodyEncoded.subString(bodyEncoded.length()-4,bodyEncoded.length());
        Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
        HttpRequest tmp = new HttpRequest();
        String last4BytesFooter = tmp.getBody()+footer;   
        bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.subString(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
        bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);

    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', authorizationData);
    req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);

    Http http = new Http();
    HTTPResponse res = http.send(req);

Code modified from this blog post


Unfortunately you’re SOL when it comes to that sort of limit, your only route is going to be moving the operation off Salesforce.

One option would be to create a Heroku app with a single endpoint that takes the attachment Id. The endpoint can then retrieve the attachment and then upload it to the 3rd party server.

Source : Link , Question Author : NSjonas , Answer Author : Frans

Leave a Comment