1

I have e response from a SOAP API which looks like the following

<GetReportResult>
        <Xml>
           <report xmlns=""/>
        </Xml>
        <Html/>
        <Files>
           <File>
               <Bytes>(pdf bytes are here)JVBERi0xLjcKJeTjz...</Bytes>
               <ContentType>application/pdf</ContentType>
               <FileName>PayNet Credit History Report - 
               113196132.pdf</FileName>
               <FileSize>41262</FileSize>
           </File>
        </Files>
        <ErrorCode>0</ErrorCode>
        <ErrorDescription/>
     </GetReportResult>

How can I save these bytes, which represent the pdf file, to a sObject? I know that attaching is done via Attachment object I successfully save plain text but the same code does not work for the pdf.

 public void AttachResponseToOpportunity(String xmlResponse, String contentType, String fileName, Id parentId, Boolean isPrivate) {
    Attachment attachment = new Attachment();

    Blob body = Blob.valueOf(xmlResponse);

    attachment.Body = body;
    attachment.Name = fileName;
    attachment.IsPrivate = isPrivate;
    attachment.ContentType = contentType;
    attachment.ParentId = parentId;

    List<SObject> attachments = new List<SObject> { attachment };
    UpsertSObjects(attachments);
}

I found a couple of articles that show how I can save a VF page as pdf but none of them helps me because I have the pdf in bytes, not as a page.

2

Hello after a bit more research I found out how to save the pdf bytes as an Attachment object.

This is the code:

 public void AttachResponseToOpportunity(String pdfBytes, String contentType, String fileName, Id parentId, Boolean isPrivate) {
    Attachment attachment = new Attachment();

    attachment.Body = EncodingUtil.base64Decode(pdfBytes);
    attachment.Name = fileName;
    attachment.IsPrivate = isPrivate;
    attachment.ContentType = contentType;
    attachment.ParentId = parentId;

    List<SObject> attachments = new List<SObject> { attachment };
    UpsertSObjects(attachments);
}

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.