Monday 20 January 2014

Dynamic attachment in email template

In salesforce email templates, we can send attachments which are predefined or in the salesforce standard Send email UI we can chose the attachment from computer files.
For static/fixed pre defined attachment in email template we can simply add the attachment in the related list of the email template.



This will always send the same attachment that has been configured. We can even send dynamic attachment in case of visualforce email templates, we can code the attachment as we wish to make it dynamic(The attachment content will be dynamic). Say you want to put some content in the attachment which draws record field values.

create a visualforce email template, then edit its contents.As an example following is the default vf email template code that you would see when you edit the template-

<messaging:emailTemplate subject="Product Enquiry" recipientType="Lead" >
  <messaging:plainTextEmailBody >
    Congratulations!
    This is your new Visualforce Email Template.
  </messaging:plainTextEmailBody>
</messaging:emailTemplate>

Now to add attachment dynamically, use <messaging:attachment> attribute, you can render the attachment as wished, the default format is .txt

<messaging:emailTemplate subject="Product Enquiry" recipientType="Lead" >
  <messaging:plainTextEmailBody >
    Congratulations!
    This is your new Visualforce Email Template.
  </messaging:plainTextEmailBody>
  <messaging:attachment filename="yourCases.txt">
     test document in vf page attachment
  </messaging:attachment>
</messaging:emailTemplate>

This would give you a visualforce attachment as related list to the email template


Thus whenever this email template would be used, the visualforce attachment would be send along with the email template.

No comments:

Post a Comment