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
mauricio.ramos@corpitalmauricio.ramos@corpital 

URGENT: add an attachment (PDF) to a visualforce template. HOW TO

I have an urgent requirement to create a visuaforce template (or other similar solution) where I can email an attached PDF from a custom object. Basically I have a Sales Order object and create a pdf for that record then would want to email it from a button. I know I can code a visualforce page with all the code logic but did not have enough time to complete. Therefore I am using the EmailAuth functionality from Salesforce BUT I do not know how to add this PDF attachment to the email. 

 

I tried using the doc_id= parameter but it receives a document record and I have an attachment instead. I therefore created a visualforce template but I have no idea how to use the messaging:attachment tab to add the PDF to the email.

 

Below is what I have created

 

The custom button code:

"/_ui/core/email/author/EmailAuthor?p2_lkid="{!SCRB_SalesOrder__c.Selling_ContactId__c}"&rtype=003&p3_lkid="{!SCRB_SalesOrder__c.Id}"&new_template=1&template_id=00XM0000000Dglk

 

VF email template:

<messaging:emailTemplate subject="Sales Doc Email PDF" recipientType="Contact" relatedToType="SCRB_SalesOrder__c">
<messaging:plainTextEmailBody >


Dear {!recipient.FirstName} {!recipient.LastName},

I have attached the {!relatedTo.Id}, as we discussed. 

Please call me if you have any questions.

Sincerely,
{!$User.FirstName} {!$User.LastName}
{!$User.Phone}


</messaging:plainTextEmailBody>

<messaging:attachment filename="Sales Doc (Quote) Attachment" renderAs="pdf"  >
   <c:PDF_Attachment SalesDoc_ID="{!relatedTo.Id}" rendered="true"> </c:PDF_Attachment> 
</messaging:attachment>

</messaging:emailTemplate>

 

VF component:

<apex:component controller="SalesDoc_PDFAttachment_Controller" access="global" >
<apex:attribute name="SalesDoc_ID" Description="reference to Parent Sales Document" type="ID" assignTo="{!salesDoc_ID}" />
</apex:component>

  and the controller for the component: 

global class SalesDoc_PDFAttachment_Controller {

SCRB_SalesOrder__c SD {get;set;}
Blob salesDoc_PDF {get;set;}
Attachment att {get;set;}
Id salesDoc_ID {get;set;}



    global SalesDoc_PDFAttachment_Controller() {
    system.debug('###PDF Attach -SD id: ' + salesDoc_ID ); 
    //Set parent Sales Doc
    SD = [SELECT id, Active_PDF_Attachment_ID__c FROM SCRB_SalesOrder__c WHERE Id = :salesDoc_ID LIMIT 1];
    salesDoc_ID = SD.id;
    
    if(SD <> null) {
    //get attachment if exists;
    Attachment att = [SELECT id, Body FROM Attachment WHERE id= :salesDoc_ID LIMIT 1 ];
    salesDoc_PDF = att <> null ? att.Body : null;
    System.debug('##PDF Attach -att: ' + att);
    }
    }

}

 

This code gives me a "List has no rows for assignment to SObject" error but I am sure I am not doing things correctly.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

I think you need to modify your query to...

 

Attachment att = [SELECT id, Body FROM Attachment WHERE ParentId= :salesDoc_ID LIMIT 1 ];

 

 

kareem zokkareem zok
Hello , 
I was trying this code 
<messaging:emailTemplate subject="Approval required for" recipientType="User" relatedToType="Contract">
<messaging:htmlEmailBody >
Please find attached file for review  
</messaging:htmlEmailBody>aa
<messaging:attachment renderAs="PDF" filename="file.pdf">
     <c:SitePoweredBy /> 
</messaging:attachment>
</messaging:emailTemplate>
And the component is 
 
<apex:component id="poweredbyComponent"  access="global">
  <apex:panelGrid cellpadding="0" cellspacing="0" width="100%" border="0" style="text-align: right;" columns="1" id="thePoweredByTop">
    <apex:panelGroup id="poweredBy">aa
      <apex:outputText style="font-size: smaller" value="{!$Label.site.powered_by}"/>
      <apex:outputLink value="http://www.force.com"><apex:image url="" styleClass="poweredByImage"/></apex:outputLink>
    </apex:panelGroup>
   </apex:panelGrid>
</apex:component>

When i add this to the email template and run the test email , the mail is sent but not attached to the mail .. what i m doing wrong?