function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ishan K SharmaIshan K Sharma 

Attach pdf(vf page) to attachments of email functionality of Activity

Hi

 

i have scenerio to which i am stuck up with. I need help with it.

 

I created a vf page render as pdf type which have two buttons on top SAVE and SAVE&EMAIL. On click of SAVE&EMAIL  it will save as attachment of notesandattachment of some specific object  and also will redirect to send email page of Activity and this vf page(pdf type) should be attached as an attachment of it automatically. So that we dont need to attach file manually and can send it as attachment of email by just hitting the send email button.

 

I am able to attach it to Notes and attachments but i am stuck up with 2nd scenerio. I need help with it. What should be the approch.

 

Thanks 

Ishan Sharma

Ana Catarina BritesAna Catarina Brites

Hello 

 public PageReference sendPdf(String Email) {
    
    Blob body;
    PageReference pdf = Page.VisualforcePDF; // the name of your Visual Force page
    //In case you need to add Parameters to your page
	pdf.getParameters().put('id',accountId);
	
    try { 
      // returns the output of the page as a PDF
      body = pdf.getContent();
 
    // need to pass unit test
    } catch (VisualforceException e) {
      body = Blob.valueOf('Some Text');Spell Check
    }
 
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType('application/pdf');
    attach.setFileName('PDFName'.pdf');
    attach.setInline(false);
    attach.Body = body;
    
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    mail.setToAddresses(Email);
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
    mail.setSaveAsActivity(true); // to save as an activity or you can set this to false and create a task by yourself
    
    // Send the email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    
    return null;
}

 

If you decide to create the task yourself, just she the description to:

 

Description = mail.getPlainTextBody();

Hope it helped.

ranendu banerjeeranendu banerjee
Generated pdf is not opening. saying either it is corrupted or not correctly encoded.
ranendu banerjeeranendu banerjee

@Ana Catarina Brites....Could you explain this please ?