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
ChubbyChubby 

Dynamic Subject in visualforce email template based on value of field

Hi all,

I have a requirement in visualforce Email template. I need to conditionally display subject for Email templates based on a field value. I found the below one in some blog.
<messaging:emailTemplate subject="{!IF(relatedTo.LeadSource = 'Web','This is PG1 subject', IF(relatedTo.LeadSource = 'Other','This is PG2 subject','false') )}"  recipientType="Lead" relatedToType="Lead">
 
<messaging:plainTextEmailBody>
 
{!
IF(relatedTo.LeadSource = "Web","This is PG1 email"
,
IF(relatedTo.LeadSource = "Other","This is PG2 email ","false")
)
}
 
</messaging:plainTextEmailBody>
 
</messaging:emailTemplate>
But in the subject i want to display Leasd source as well. Subject would be like "This is PG1 Email Web"/"This is PG1 Email Other".
Please help me with this requirement.

Thanks.
Best Answer chosen by Chubby
GhanshyamChoudhariGhanshyamChoudhari
{!IF(relatedTo.LeadSource = "Web","This is PG1 email"+relatedTo.LeadSource,IF(relatedTo.LeadSource = "Other","This is PG2 email ","false"))}

 

All Answers

bhanu_prakashbhanu_prakash
Hi Chubby,

Mark as best answer, If it resloves 

You need to design custom controller class where you can store lead source and required fields and form that controller you can call in visualforce page or Lightning component.

check below link for lightning component example 
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

Thanks, 
Bhanu Prakash
visit ForceLearn.com  (https://www.forcelearn.com)
GhanshyamChoudhariGhanshyamChoudhari
{!IF(relatedTo.LeadSource = "Web","This is PG1 email"+relatedTo.LeadSource,IF(relatedTo.LeadSource = "Other","This is PG2 email ","false"))}

 
This was selected as the best answer
ChubbyChubby
Hi GhanshyamChoudhari,
Thanks for your reply. I tried this but not working.
Thanks.
 
ChubbyChubby
Hi GhanshyamChoudhari,

I have tried the below code and it's working.

{!IF(relatedTo.LeadSource = 'Web','This is PG1 email'+relatedTo.LeadSource,IF(relatedTo.LeadSource = 'Other','This is PG2 email'+relatedTo.LeadSource,'false))}

But it is displaying the subject as This is PG1 emailWeb. There is no space between email and Web. I am still trying to apply different methods for this.

Thanks.
GhanshyamChoudhariGhanshyamChoudhari
{!IF(relatedTo.LeadSource = "Web","This is PG1 email "+relatedTo.LeadSource,IF(relatedTo.LeadSource = "Other","This is PG2 email ","false"))}
Above you can try.
mark answers as best answer if it helps you
ChubbyChubby
We can not use double quotes in side IF conditions. This is syntax error. I gave single quote and its worked. But it is displaying the subject as This is PG1 emailWeb. There is no space between email and Web
GhanshyamChoudhariGhanshyamChoudhari
{!IF(relatedTo.LeadSource = 'Web','This is PG1 email   spaceadded '+relatedTo.LeadSource,IF(relatedTo.LeadSource = 'Other','This is PG2 email ',"false"))}

add space after email word.
 
ChubbyChubby
Hi GhanshyamChoudhari,

I could not have posted here if just adding space after the word email works. I tried that before posting here.

Thanks.
ChubbyChubby
We should add space before closing single quote. Finally I got it with your help.
Thanks a lot GhanshyamChoudhari. Marked your answer as best.