Why is the IF Condition Not Working in My Email Template?

Question

I added an IF condition to my email template, but it is not working as expected.

{IF({{{Custom__c.is_custom_true__c}}}, 'Yes', 'No')}  

Instead of evaluating the condition, the output appears as:

IF(1, 'Yes', 'No')  

The expected output should be:

Yes  

Additionally, when I try using this IF statement:

{!IF(Custom__c.is_custom_true__c, "Yes", "No")}  

I get the following error message:

“This template doesn’t support SML merge fields. Use the merge field picker to replace merge fields starting with ‘{!’.”

How can I correctly format the IF condition in my email template?

Answer

In Lightning Email Templates, you need to use Handlebars Merge Language (HML) instead of the standard merge field syntax. The correct way to write an IF condition in HML is:

{{#if Custom__c.is_custom_true__c}}  
Yes  
{{else}}  
No  
{{/if}}  

This syntax ensures that the condition evaluates correctly and displays either “Yes” or “No” based on the field value.

Understanding the Issue

If you cannot use HML or prefer a simpler approach, you can create a formula field on the object that evaluates the condition and returns the desired text.

For example, create a formula field Formatted_Value__c on Custom__c with the following formula:

IF(Custom__c.is_custom_true__c, "Yes", "No")  

This syntax is incorrect because Salesforce email templates do not use this format to evaluate conditions. Instead of processing the IF statement, Salesforce treats it as plain text and displays it in the email output. Then, reference this formula field directly in your email template:

{{Custom__c.Formatted_Value__c}}  

This syntax uses the Salesforce Merge Language (SML), which is supported in Classic Email Templates but not in Lightning Email Templates. When using it in a Lightning Email Template, you get the error:
“This template doesn’t support SML merge fields. Use the merge field picker to replace merge fields starting with ‘{!’.”

Correct Approach Using HML in Lightning Email Templates

Lightning Email Templates use Handlebars Merge Language (HML), which has a different way of writing conditions. The correct syntax is:

{{#if Custom__c.is_custom_true__c}}  
Yes  
{{else}}  
No  
{{/if}}  

This ensures that:

If Custom__c.is_custom_true__c is true, it displays Yes.
If Custom__c.is_custom_true__c is false, it displays No.

Why Use HML?

Salesforce is standardizing on HML for Lightning Email Templates because it provides more flexibility and a better user experience. Unlike older methods, HML allows you to write structured conditional logic directly in the template without requiring additional formula fields or custom logic.

Alternative Approach: Using a Formula Field

If you prefer to keep the email template simpler or if you are using an email template type that does not support HML, you can create a formula field in Salesforce.

  1. Create a new formula field (Formatted_Value__c) on Custom__c.
  2. Use this formula:
IF(Custom__c.is_custom_true__c, "Yes", "No")  

3. In your email template, simply reference this formula field:

{{Custom__c.Formatted_Value__c}}  

This approach ensures that Salesforce evaluates the condition before the email is generated, eliminating the need for complex template logic. It also works across different email template types, including Classic and Lightning Email Templates.

Which Approach Should You Use?

If you’re using Lightning Email Templates, use the HML syntax ({{#if ...}}).

If you’re using Classic Email Templates, use a formula field or the older merge field syntax ({!IF(..., ..., ...)}).

If you want the logic to be handled within Salesforce rather than in the email template, use a formula field and reference it in your email template.

By following these best practices, you can ensure that your email templates work correctly across different scenarios in Salesforce.

Enroll for Career-Building Salesforce Training with Real-Time Projects

Our Salesforce course is designed to provide you with a comprehensive understanding of the Salesforce platform, empowering you with the essential skills to thrive in the CRM industry. The course covers key topics such as Salesforce Admin, Developer, and AI, seamlessly integrating theory with practical application. By engaging in hands-on projects and assignments, you’ll develop the expertise to solve real-world business challenges using Salesforce solutions. Our expert trainers ensure you gain the technical knowledge and industry insights needed to excel in the Salesforce ecosystem.

In addition to technical skills, our Salesforce training in Mumbai offers personalized mentorship, certification guidance, and interview coaching to support your career growth. You’ll have access to a wide range of study materials, live projects, and dedicated support throughout the course. By the time you complete the program, you will be well-prepared for certification exams and equipped with the problem-solving skills that employers value. Start your Salesforce career today and unlock a world of opportunities. Sign up for a free demo now!

0
Would love your thoughts, please comment.x
()
x