1

I'm encrypting LiveChat Transcript body using integration with Skyhigh and storing it in the record. After that I'm trying to insert this as an attachment.

Problem: Body is getting stored as in encrypted format say something like this enter image description here

But when I try to insert as an attachment it only stores the character.

enter image description here

Code for this is

 String pdfContent = '' + FORM_HTML_START;
        String cent = 'center'; 
        String pdfContentUCSW = '';
        try
        {
           System.debug('Encrypted Body :'+encryptedBody);

            pdfContent = '' + FORM_HTML_START;
            pdfContent = pdfContent + '<H2 align="center">Chat Transcript</H2>';
            pdfContent = pdfContent + '<P  style="font-family: Arial Unicode MS;">' +encryptedBody+ '</P>';
            pdfContentUCSW = pdfContent+'<P>'+chats.Body+'</P>';
            pdfContent = pdfContent + FORM_HTML_END;
            pdfContentUCSW = pdfContentUCSW + FORM_HTML_END;

            System.debug('Pdf --> :'+pdfContent); //here it shows the encrypted data

            Attachment attachmentPDF = new Attachment();
            attachmentPDF.parentId = chats.Id;
            attachmentPDF.Name = chats.Name + '.pdf';
           // attachmentPDF.ContentType = 'application/pdf;charset=UTF-8;';
      //System.debug(Blob.toPDF(pdfContent).toString());
      //Blob.valueOf(pdfContent);

            attachmentPDF.body = Blob.toPdf(String.valueOf(pdfContent)); //This creates the PDF content
            insert attachmentPDF;
}

How can I overcome this?

  • 1
    You seem to be seeking to embed binary encrypted data within a UTF-8-encoded HTML string. That doesn't really make sense to me - why render binary data that's not readable? And it's not surprising that it breaks things, since raw binary data is not valid UTF-8 or HTML or PDF content. – David Reed Jun 5 '18 at 13:46
  • 1
    Why not simply include the encrypted data blob as the attachment body? – David Reed Jun 5 '18 at 13:47
  • @DavidReed removed the binary code and simply inserted attachment. But when I try to append it as pdf it is not viewing anything. But when i try to open in a word document it is showing the encrypted text. Does pdf doesnt support the encrypted text? – Eagerin Sf Jun 5 '18 at 14:47
0

Encrypted data, unless you proactively encode it in Base64, is raw binary. You cannot reliably include raw binary data in a Salesforce text field, which expects valid UTF-8 encoded, human-readable data. If you do this, the best outcome you can expect is the gobbledygook in your screenshot, which represents Salesforce trying and failing to decode your encrypted blob as if it were UTF-8.

More or less similar issues apply if you attempt to embed this binary blob in HTML or in PDF. While the behavior may vary somewhat depending on the client and the specific format in which you place the data, as a general rule, you cannot stuff binary into a text format and expect it to work in any comprehensible way. Text formats are not containers for binary data.

You can, however, attach an arbitrary data blob as an attachment. You do not need to convert it to either PDF or HTML, and it's not clear why you would want to. Once you have your encrypted data in a variable of type Blob, you can assign it directly to the Body property of an Attachment object. You can assign whatever filename you wish, perhaps with the extension .txt.enc to indicate it's encrypted.

| improve this answer | |

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.