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
DevWannabeDevWannabe 

Passing Values to an Email Attachment

I have written a trigger on Contacts that sends an email to that contact and references an email template that has a Visualforce page redered as a pdf as an attachment.  I am trying to pass values from the contact into the VF page that is being attached, but when I try it none of the fields get put in there.  Here is my trigger...

trigger MCR on Contact (after update) {

List<id> clist=New List<id>();
id crecord=trigger.new[0].id;

for(contact c:trigger.new){

 if(c.M_C_R__c==true)
   clist.add(c.id);
 }
 
 List<contact> c=[Select id, accountid From contact Where ID IN: clist];
 
  for ( contact csend:c){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTargetObjectId(csend.id);
    mail.setTemplateId('00Xd0000000PA6n');
    mail.setWhatId(csend.accountid);
    mail.saveAsActivity = true;
    Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
  }
}

 The email goes out with the attachment and it gets put in the Contact Record's Activity History related list, but when I open the attachment in the email none of the fields from that Contact Record are inserted.  If it's not in the code, then it might be how I am referencing the fields in the VF page.  I am using {!Contact.Name} to try and pull in the name of the contact among other things, but that gives you an idea.

 

Any help is greatly appreciated...

carlocarlo

If you send an email via the web UI using the template does the VF page display correctly in the email?

DevWannabeDevWannabe

No it doesn't.  The merge fields in the VFPage that is attached are as follows...

 

<apex:outputField value="{!Contact.name}"/>

 

carlocarlo

Can you paste the code for your VF page here.

DevWannabeDevWannabe

I also have VF issue where I am displaying a card that the recipient can print off and cut out, but I can't seem to center it.  It's at the bottom of the page where I am trying to merge the info from the contact.  The centering part doesn't matter much if I can't get the issue we are discussing resolved, so here it is...

 

 

<apex:page standardController="Contact" showHeader="false" renderAs="pdf">
<apex:stylesheet value="{!URLFOR($Resource.pdfStyle)}"/>
  <html>

    <body>
  <table align="center" border="0" cellpadding="0" width="620">
  <tbody>
  <td colspan="5" height="100">
  <apex:image url="{!$Resource.EMCHeader}" width="620" height="90"/></td>

    <br/>
    <br/>
    <table align="center" border="0" cellpadding="0" width="620">
    <p style="font-family: Courier; font-size:16px;">Some text Here.</p>
    <p style="font-family: Courier; font-size:16px;">Some more text here.</p>
    </table>
    <br/>
    <table horizontal-align="center">
    <p style="font-family: Courier; font-size:16px; text-align:center;">Street, City, State Zip</p>
    <p style="font-family: Courier; font-size:16px; text-align:center;">phone / website</p>
    <br/>
    <br/>
    <p style="font-family: Courier; font-size:10px; text-align:center;">(Please find your membership card below)
---------------------------------------------------------------------------------------------------------------------------
</p>
    </table>
    <body>
    <g style="horizontal-align=center;">
    <apex:panelGrid columns="1" style="font-family:Courier; text-align:center; width:400px; border:dashed; ">
     <table align="center" border="0" cellpadding="0" width="300">

     <td colspan="5" height="10">
     <apex:image url="{!$Resource.EMCCardHeader}" width="300" height="35"/></td>
      <apex:panelGroup >
       <p style="font-family: Courier; font-size:18px; font-weight:bold; text-align:center;">
       <apex:outputField value="{!Contact.name}"/></p>
      </apex:panelGroup>
      <apex:panelGroup >
       <apex:outputField value="{!Contact.Membership_Classification_for_Template__c}"/>
      </apex:panelGroup>
      <apex:panelGroup >
       Member #&nbsp; <apex:outputField value="{!Contact.Member_ID__c}"/>
      </apex:panelGroup>
      <apex:panelGroup >
       Proud Member Since &nbsp; <apex:outputField value="{!Contact.Member_Since__c}"/>
      </apex:panelGroup>
      
      <br/>
      <br/>

       <p style="font-family: Courier; font-size:16px; text-align:center;">
      <apex:panelGroup >
        Street, City, State Zip <br/>
      </apex:panelGroup>
      <apex:panelGroup >
        Phone Number
      </apex:panelGroup>
       </p>

    </table>
     </apex:panelGrid>
     </g>
     </body>
  </tbody>
  </table>
  </body>
  </html>
</apex:page>
                     

 

carlocarlo

This is how you write a VF email template.

 

<messaging:emailTemplate subject="vf test" recipientType="Contact" >
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
{!recipient.Name}
</messaging:plainTextEmailBody>
<messaging:htmlEmailBody >
Congratulations!<br/>
This is your new Visualforce Email Template.<br/>
<apex:outputField value="{!recipient.Name}"/>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

DevWannabeDevWannabe

What I sent you was the VF page for the attachment.  I used that as the VF attachment in the template as I have tried to include that in the format you just presented and it won't let me.  So unless I can somehow incorporate the VF code I sent you in the VF email template code without losing its format I will need to determine some other way of accomplishing this task.  The additional issue is that we want to add a copy of the attachment to the record we are sending from, so with these two situations combined I was thinking there would be a way between a trigger and a class that I could create the pdf (pulling in the fields from the contact) attach it to the contact record and send it out in an email as an attachment and keep it as an activity on the contact as well.

carlocarlo

I'm guessing here.

 

I've never created an attachment to an email which is a vf page.

 

I would guess the attachment needs to be static.  As in a static file which never changes and can not be passed values.  But again I'm guessing.

 

I would try generating the pdf and saving the output as a static file.  If you can do that then you can attach the file to an email and send it.

 

But as I'm guessing I could be way off.