• kareem zok
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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.