This section describes the technical tricks you can use in Email template. One of the most common questions I’m asked is : How do I have conditional fields displayed in an email template. Believe it or not you can use IF() function provided by Salesforce. This function is not an APEX code or Visual Force function but in fact, the same function you use for Validation rules and Workflows. If you want to have a conditional value in an email template you just use IF() within the Merge field itself like so:

{! IF( Show_This_Field__c = TRUE, “This is a value to display”, “”) }

To put it in a more defined example, consider that you want to display Home Phone, Cell Phone and Fax for a Contact record. The email template has the following:

Home Phone : {!Contact.HomePhone}
Cell Phone : {!Contact.Phone}
Fax : {!Contact.Fax}

Now we can make each of the fields (and there label) visible with a simple value IF() check. NOTE that for this to work the IF() clause must appear within the merge field brackets after the “!”). So we move the label inside the merge field as well, like so:

{! IF( Contact.HomePhone  = “”, “”, “Home Phone :” + Contact.HomePhone) }
{! IF( Contact.Phone  = “”, “”, “Cell Phone :” + Contact.Phone) }
{! IF( Contact.Fax  = “”, “”, “Fax :” + Contact.Fax) }

That’s all there is to it. Now you can conditional control both content and labels.

[contact-form]
Filed under: Salesforce.com Programming