1

I have the following function which generated an email based on a template passed. The function is triggered from a rest api.

public static Messaging.SingleEmailMessage singleMessageHelper(
    bookingsWrapper wrap, String emailBody, String subject, 
    Id targetObjectId, Boolean saveAsActivity, Id templateId
){
    System.debug('singleMessageHelper : ' + emailBody);
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setSubject(subject.replace('{YYYY}', String.valueOf(System.Today().year())));
    email.setTargetObjectId(targetObjectId);
    email.setSaveAsActivity(saveAsActivity);
    email.setTemplateId(templateId);
    email.setUseSignature(false);
    emailBody = emailBody.contains('delegateName') ?
        emailBody.replace('delegateName', wrap.delegateName) : 
        emailBody;

    if(emailBody.contains('seeAttachment')){
        System.debug('hasAttachment : yay');
        PageReference pdf =  Page.OB_JoiningInstructionsTemplate;
        pdf.getParameters().put('Id', wrap.opportunityId);
        pdf.getParameters().put('bookingId', wrap.bookingId);
        pdf.getParameters().put('isNebosh', String.isBlank(wrap.seriedId) ?
            'false' : wrap.seriedId);
        pdf.setRedirect(true);
        Blob pdfBody;
        Boolean isError = false;
        System.debug('page : ' + pdf);
        try{
            pdfBody = pdf.getContent();
            Attachment attach = new Attachment();
            attach.Body = pdfBody;
            attach.Name = 'Joining_Instructions-' + wrap.oppName + '.pdf';
            attach.IsPrivate = false;
            attach.ParentId = wrap.bookingId;
            insert attach;
            System.debug('pdfBody : ' + String.valueOf(pdfBody));
            isError = false;
        }catch(Exception e){
            isError = true;
        }
        if(isError){
            email = null;
            //doFutureEmailSend(wrap.bookingId);
        }else{
            String filename = 'Joining_Instructions-' + wrap.oppName + '.pdf';
            Messaging.EmailFileAttachment emailAttachment = new Messaging.EmailFileAttachment();
            emailAttachment.setContentType('application/pdf');
            emailAttachment.setFileName(filename);
            emailAttachment.Body = pdfBody;
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {emailAttachment});
            emailBody = emailBody.replace('seeAttachment', '');
        }
    }
    if(email != null){
        if(emailBody.contains('ccBooker')){
            email.setCcAddresses(new List<String>{wrap.bookerEmail});
            emailBody = emailBody.replace('ccBooker', '');
        }
        email.setHtmlBody(emailBody);
        System.debug('templateId : ' + templateId);
        System.debug('mail.getHTMLBody() : ' + email.getHTMLBody());
    }
    return email;
}

This works fine and sends the emails with a pdf attachment. But when I try to open the pdf file, it says it is of invalid format. The VF page is a very basic one

<apex:page showHeader="false" sidebar="false" standardStylesheets="false" cache="false" standardController="Opportunity" extensions="OB_JoiningInstructionsTemplateCont" applyHtmlTag="false" renderAs="pdf">
    <p>this is a paragraph</p>
</apex:page>

I am calling this function from a Queueable class. However, if I bypass using the rest route, and just execute the same code from developer console, it sends the right format. I've been on this for a good number of hours now and still can't figure it out

2

You seem to be missing to set the ContentType on your Attachment being created. To be able to provide the right "file type" of the attachment that you are inserting, you need to provide that value. Also make sure that when you set the Body of the attachment, that's converted to PDF too. So your code will look like as below:

Attachment attach = new Attachment();
// my existing code
....
attach.Body = Blob.toPdf(myStrBody);
attach.ContentType='application/pdf';
insert attach;
  • the things is i'm not actually using the attachment object for the email attachment itself. I was just inserting it. it was one of the things I wanted to try before I posted this question. but i will give it a try now.. thx – saint Sep 26 '18 at 8:16
  • unfortunately, that didn't work either. It attaches to the record by the format is still invalid – saint Sep 26 '18 at 8:41
  • You need to make sure you have the body and contenttype both set as mentioned. Note that with a quick test, I was able to replicate your issue on attachments on a record not opening and by adding this, it worked fine. – Jayant Das Sep 26 '18 at 10:15

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.