+ Start a Discussion
dmchengdmcheng 

Pass parameters into VF email template?

Hello.  I have a client who manages seminars and speakers using Campaigns.  The client wants a custom button on Campaign detail screen that will send two emails to each campaign member:

 

Email 1 contains fields from Campaign and related Account (seminar location map and directions text).

 

Email 2 contains a list of seminar speakers' contact info.  This list of info contains fields from Contact, Campaign and CampaignMember.  In other words, if John Doe, Richard Roe, and Nancy Smith are members in a campaign, the button should create a list containing info from Contact and CampaignMember for these  three contacts, and then email the list to each one of them.

 

First I tried using standard email template but I can't pass the campaign and campaignmember fields.

 

Now I'm looking at Visualforce email templates but I can't see how I can pass the Campaign Id/Name to the VF email template.  I need the campaign ID because I  need a VF component to display the contact list and so I have to pass the Campaign ID through the VF template into the component.

 

Can anyone tell me if parameters can be passed to VF email templates?  I've looked at the documentation and done some searching but haven't found anything.

 

Thanks

David

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You don't need to have a direct relationship in VF template world as far as I recall - that's the main reason I go that route. 

 

Just to be clear though, I am referring to the generation of an email using a Visualforce template from Apex code.  If you are using it via workflow or similar, I think you'll need to have the relationship. Otherwise I can't see how you could configure it, as you normally have to specify the who to send the email to based on fields in the object.

 

However, there's nothing to stop you having a custom component and controller that pulls in unrelated records.

All Answers

bob_buzzardbob_buzzard

You can pass two parameters into a Visualforce email template - the targetObjectId (recipientType) and the what Id (relatedToType).  If you need to reach other sobjects, you'd do this via a custom component with an associated custom controller.  You can pass information from the template (e.g. the relatedTo) to the component via an attribute, and then pull in whatever related objects you require via Apex.

 

dmchengdmcheng

Hi Bob, thanks for your reply.  If TargetObjectId is a Contact, can I put a Campaign into WhatID?  That's the part I'm having trouble envisioning - it seems like that should not work because there's no direct relationship between Contact and Campaign.  The documentation examples always seem to use direct relationships like Contact -> Cases, etc.

bob_buzzardbob_buzzard

You don't need to have a direct relationship in VF template world as far as I recall - that's the main reason I go that route. 

 

Just to be clear though, I am referring to the generation of an email using a Visualforce template from Apex code.  If you are using it via workflow or similar, I think you'll need to have the relationship. Otherwise I can't see how you could configure it, as you normally have to specify the who to send the email to based on fields in the object.

 

However, there's nothing to stop you having a custom component and controller that pulls in unrelated records.

This was selected as the best answer
dmchengdmcheng

Yes, I'm using a custom button calling a VF controller extension so I can pass both Contact and Campaign.  Thanks I'll give it a try.

SwayampatiSwayampati

Hey did you able to figure this out. I am still stumbling on this one. I can't pass the custom object id to the email template. Please help if you know it.

 

Thanks