1

I'd like to retrieve the size of quote document without retrieving actual content. Say, for attachments it's pretty easy:

SELECT BodyLength FROM Attachment WHERE Id = 'something'

But QuoteDocument object in Salesforce doesn't have any size related field, only Document content itself. So, my question: is there is method to retrieve size of quote document without retrieving the content?

3

I believe if you setup your specific salesforce instance URL under the Remote Site Settings you should be able to utilize the HttpRequest/HttpResponse classes in Apex to call out to REST. The REST interface should then be able to bring back the ContentSize of the specific Document/Attachment that you have, without actually loading the file into memory.

If you go down that path take a look here which will get you started: https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

Then since you are already logged into Salesforce you should be able to use the existing SessionID in your HTTP Headers. I believe that Userinfo.getSessionId() will do that trick. I don't have any code to share on this, but hope if you decide to go this route that this helps.

  • Didn't occur to me to do it as a callout from Apex, nice one! – Matt Lacey Jan 23 '15 at 6:27
1

It's not exactly pretty but you could get some idea of the size but just checking the length of the Document field (though you'd need to know it's not going to blow out your heap space).

There's an answer on StackOverflow which has a method to get an upper bound for the size of a base64 encoded file:

The exact length cannot be calculated unless you look at the padding. Without looking for padding, the best you can do is calculate an upper bound for the length by multiplying the encoded-string length with 3/4 (the encoded length is guaranteed to be exactly divisible by 4).

The upper bound calculated thus will be either N, N+1 or N+2, where N is the length of the raw data.

One way to not blow the heap would be to do this outside of the platform, potentially via Javascript in a Visualforce page for instance, or (stretching it a little) do it somewhere like Heroku and update some field that indicates the size of each document back in the org.

  • Agreed, but that means I have to read it, i.e. load to memory and use some heap. Document can be large. – Mike Raven Jan 22 '15 at 23:49

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.