How to use an if-statement in SalesForce E-Mail Template

MaxiNet picture MaxiNet · Jan 16, 2013 · Viewed 7.1k times · Source

I'm trying to add a if-Statement to my E-Mailtemplate (Type: HTML). I used the following code:

{!if(Lead.LastName="", "1", "2")}

My Output is just nothing :/

Does anyone know how to do it right?

Answer

eyescream picture eyescream · Jan 16, 2013

I believe the more complex logic like conditionally rendering data is not available in straightforward emails. IF, BLANKVALUE etc functions don't work, instead there's something similar to Dear {!Lead.LastName, 'Sir or Madam'} that's supposed to be fallback scenario if the field was empty.

If you're not too afraid - make a visualforce email template instead?

<messaging:emailTemplate subject="Hello StackOverflow" recipientType="User" relatedToType="Lead">
    <messaging:HtmlEmailBody >
        <p>Related To: {!relatedTo.Name}, {!relatedTo.LeadSource}</p>
        <p>Recipient: {!recipient.Username}</p>

        <p>{!IF(ISBLANK(relatedTo.LeadSource), '1', '2')}</p>
    </messaging:HtmlEmailBody>
</messaging:emailTemplate>