We are querying for contentversion data in apex and would like to take the first part of the file data and send chunk by chunk to another webserver.

Is there a way to only take 10,000 bytes of the blob versionData. Does anyone have any example apex that does this?

Currently we are able to query, but as soon as refer to the data field we blow our heap size (it's a big file).

We would like to adjust the code below to only take chunks in the second line of apex...

contentversion[] cv=[select id, title, versiondata from contentversion where title like 'Deadmau5%'];
String att64 = EncodingUtil.base64Encode(cv[0].VersionData);

I don't think it is currently possible to chuck blob data.

I've had a similar issue with the Attachment body - Salesforce LimitException: Apex heap size too large.

Right now I've done two things to help:

  1. Move to a Batchable implementation with the batchSize/scope set to 1. That way you will only ever be dealing with a single Attachment/ContentVersion (reducing the overall heap size). Plus the batch context has a larger heap size (12 MB rather than 6 MB).
  2. Check if the base64 encoding of the blob is going to push you past the heap size limit.

It isn't foolproof by any means, but can help avoid hitting the limit.

if(Limits.getHeapSize() + att.BodyLength > Limits.getLimitHeapSize()) {
    // Do something else

Failing that, you might be better to pull the binary data into the external system rather than push it in. You could callout to a webservice from Apex with the ContentVersion.Id and Salesforce Session details. Then the webservice can establish a session back, with say the partner API, and pull the blob data down with a retrieve call.

  • I agree with pulling the data into the external system rather than pushing it in --- you can use the REST API to retrieve the ContentVersion's data, e.g. with curl https://na1.salesforce.com/services/data/v20.0/sobjects/ContentVersion/001D000000INjVe/VersionData -H "Authorization: Bearer token" – zachelrath Nov 20 '12 at 2:37
  • We don't own the external webservice so we can't pull from there. Too bad – James Nov 22 '12 at 0:24
  • You could make an intermediate web service, but it starts to get a bit ugly then as it is another component to deploy and maintain. Batchable should get you close to 6MB blob support. An email service gives you a 36 MB heap size, but I'm not sure how you could use that here and it would be hacky to maintain. – Daniel Ballinger Nov 22 '12 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.