0

We have a functionality to create leads out of Emails. To do this we have a custom class in which we extract the details of the customer from the inbound email and add the email as an attachment. This has been working for over a year now and keeping in line with direction that salesforce is taking we are trying to move away from attachments and towards using Content Files. I have replaced the existing piece of code for creating attachments with the below code. I see the file is created and linked to the lead. However, when i download it by saving it as an HTML file, the file opens up with binary text. Is there something that I am doing wrong?

public void linkAttachmentsToLead(Id leadId, Messaging.inboundEmail email) {


        Blob Body;
        if(email.htmlBody != null) {
            Body = Blob.valueOf(email.htmlBody);
        }
        else {
            Body = Blob.valueOf(email.plainTextBody);
        }

        if(email.textAttachments != null) {
            for (Messaging.Inboundemail.TextAttachment textAttachment : email.textAttachments) {

              Body = Blob.valueOf(textAttachment.body);
            }
        }

        if(email.binaryAttachments != null) {
            for (Messaging.Inboundemail.BinaryAttachment binaryAttachment : email.binaryAttachments) {

              Body = binaryAttachment.body;
            }
        }

        ContentVersion cv = new ContentVersion();
        cv.ContentLocation = 'S';
        cv.VersionData=Body;
        cv.Title = email.Subject;
        cv.PathOnClient = email.Subject;
        insert cv;



        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: cv.Id].ContentDocumentId;
        cdl.LinkedEntityId = leadId;
        cdl.ShareType = 'I';

        insert cdl;


    }

1 Answer 1

0

I'll start with a more general tip. When you have a big piece of code like that, start ripping it down to the smallest piece. For instance, remove all the body stuff and get it down to:
cv.VersionData= Blob.valueOf('hello there');
Then see if that works? If so, then change it to be only the plain text body. And then start to build back up from there.

I suspect you're specific problem is that you are overwriting Body every step of the way and you end with binary attachments. Thus if you had just plain text as the body, but had a single binary attachment (email signature image perhaps?), then the very last binary attachment would overwrite it. Depending on what you want to do, you may want to be creating multiple files for each attachment. Or you may want a single body that combines it all, again depends on what you are trying to accomplish. But right now, your code creates one using whatever is "last".

1
  • Thank William. It looks like there was some binary attachment which was causing the issue. I have updated the code to keep as if-else-if to prevent the body variable from being overwritten.
    – ReM83
    Commented Jan 9, 2018 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .