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

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">
IF(relatedTo.LeadSource = "Web","This is PG1 email"
IF(relatedTo.LeadSource = "Other","This is PG2 email ","false")
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.

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


All Answers

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

Bhanu Prakash
visit  (
{!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
Hi GhanshyamChoudhari,
Thanks for your reply. I tried this but not working.
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.

{!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
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
{!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.
Hi GhanshyamChoudhari,

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

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