0

I'm hitting the heap size limit when converting attachments to base64. I've already put it in a future and running at 3xheap. What I want to do is get the size of the attachment prior to conversion and deal with it another way, but for some reason I can't seem to find any way to get the size of it? I've searched and can't seem to find an answer to this issue... surely I'm not the lucky one!

The full picture is I get inbound emails with attachments, I do some processing around the contents and then I need to send an email out to people. So I have access to both the inbound and outbound emails, I just can't find a way to get the size of anything in bytes. I'd be happy with the total email size including attachments, or just individual attachment sizes. Any help would be much appreciated!

7
  • I'm not sure how you are processing, but querying bodylength field of an attachment would return the size of the file in bytes.
    – Seesh
    Sep 15 '15 at 18:53
  • I don't believe that works for attachments for emails (Messaging.InboundEmail & Messaging.SingleEmailMessage). This isn't an Attachment to an Object.
    – sturev
    Sep 15 '15 at 19:12
  • I'm not sure if I got you right. EmailMessage object is a parent of the Attachment. ParentId field of an attachment references to the EmailMessage.
    – Seesh
    Sep 15 '15 at 19:21
  • Sure. Give a shot and post updates.
    – Seesh
    Sep 15 '15 at 19:29
  • @Seesh -- EmailMessage sObject applies only to Email2Case and not the Apex Inbound Email Service
    – cropredy
    Sep 15 '15 at 19:44
1

Here is what I ended up with:

        Integer emailSize = email.plainTextBody.length();
        // check attachments
        if(email.binaryAttachments != null) {
            for(Messaging.InboundEmail.BinaryAttachment file : email.binaryAttachments) {
                emailSize = emailSize + file.body.size();
            }
        }
        if(email.textAttachments != null) {
            for(Messaging.InboundEmail.TextAttachment file : email.textAttachments) {
                emailSize = emailSize + file.body.length();
            }
        }
0

As per this Salesforce documentation BodyLength attribute gives you Attachment Size of the file (in bytes)..

Update

For Messaging.InboundEmail you can get all Attachments with email.binaryAttachments convert them to Attachment and get the size.

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
    for (integer i = 0; i < email.binaryAttachments.size(); i++) {
        Attachment a = new Attachment(ParentId = <Parent Id>,
        Name = email.binaryAttachments[i].filename,
        Body = email.binaryAttachments[i].body);
        System.debug(a.BodyLength);
    }
}

http://developer.force.com/cookbook/recipe/retrieving-email-attachments-and-associating-them-with-records http://sfdcsrini.blogspot.com/2014/06/what-are-email-services-in-salesforce.html

3
  • I don't believe that works for attachments for emails (Messaging.InboundEmail & Messaging.SingleEmailMessage). This isn't an Attachment to an Object.
    – sturev
    Sep 15 '15 at 19:12
  • @sturev Please check my updated answer, hope this helps.
    – Saroj Bera
    Sep 15 '15 at 19:49
  • 1
    n.b. no need to convert to Attachment, use body property (type=Blob) and then Blob method size()
    – cropredy
    Sep 15 '15 at 19:51

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.