Articles in this section

How to provide an output based on a condition using IfElse()

This article explains what the IfElse() function is and provide examples of how it can be used with the binding syntax in smart templates.  

  Prerequisites

  • Library and at least one Dynamics modules enabled.
  • Templafy Desktop and Office VSTO add-in installed on the PC.
  • Space owner access to the Templafy tenant.

What is the IfElse() function? 

IfElse() is a commonly used function that outputs values based on a condition. The function is commonly used with smart fields (with the binding syntax or the visibility), and can also be used with template settings and properties.

IfElse() logic

Syntax {{IfElse(Condition, TrueValue, FalseValue)}}
Input Condition
Output TrueValue/FalseValue

  Tip

Instead of nesting multiple IfElse() statements for multiple comparisons, we recommend Switch() or And() and Or() functions.

IfElse() function examples

Example 1 - Set visibility based on value of a text form field

Using the IfElse() function to set the visibility of a smart field based on the value of the 'Subject' question originating from the response form. The binding is:

{{IfElse(Equals(Form.Subject, "Test"), VisibilityType.Hidden, VisibilityType.Visible)}}

  Example A Example B
Input Form.Subject = "Test" Form.Subject = "Something else"
Condition True False
Output Hidden text Visible text
Example 2 - Set visibility based on value of a date form field

Using the IfElse() function together with FormatDateTime() to set the visibility of a smart field based on the value of the 'Date' question (of type Date) originating from the response form. The binding is:

{{IfElse(Equals(FormatDateTime(Form.Date,"d-M-yyyy"), ""), VisibilityType.Hidden, VisibilityType.Visible)}}

  Example A Example B
Input Form.Date = empty Form.Date = filled
Condition True False
Output Hidden text Visible text

 

  Note

  • Because Equals() can only compare fields of type text (String), you have to add FormatDateTime() to your function to set visibility based on a form field of type Date.
  • In the example the format "d-M-yyyy" is used, but this can be any format.
Example 3 - Set visibility based on value of a number form field

Using the IfElse() function together with FormatNumber() to set the visibility of a smart field based on the value of the 'Number' question (of type Number) originating from the response form. The binding is:

{{IfElse(Equals(FormatNumber(Form.Number,"#"), "1"), VisibilityType.Hidden, VisibilityType.Visible)}}

  Example A Example B
Input Form.Number = "1" Form.Number = "123"
Condition True False
Output Hidden text Visible text

 

  Note

  • Because Equals() can only compare fields of type text (String), you have to add FormatNumber() to your function to set visibility based on a form field of type Number.
  • Use this binding in the Visibility expression field to configure a Custom visibility.
  • Use the format '#'.
Example 4 - Show specific text as output based on if it equals the value of a number form field/strong

Using the IfElse() function together with FormatNumber() to set the visibility of a smart field based on the value of the 'Number' question (of type Number) originating from the response form. The binding is:

{{IfElse(Equals(FormatNumber(Form.Number,"#"), "1"), "Hello world", "The quick brown fox jumps over the lazy dog")}}

  Example A Example B
Input Form.Number = "1" Form.Number = "123"
Condition True False
Output "Hello world" "The quick brown fox jumps over the lazy dog"

 

  Note

  • Because Equals() can only compare fields of type text (String), you have to add FormatNumber() to your function to set visibility based on a form field of type Number.
  • Use this binding in the Binding field.
  • Use the format '#'.
Example 5 - Show specific text as output based on if it contains the value of a text form field

Using the IfElse() function together with Contains() to inject specific product information in the smart template based on the 'Subject' question (of type Text) originating from the response form. The binding is:

{{IfElse(Contains(Form.Subject, "Product A"), "Product A is $100", "Pricing list available upon request")}}

  Example A Example B
Input Form.Subject = "We are showcasing Product A" Form.Subject = "We are showcasing different products"
Condition True False
Output "Product A is $100" "Pricing list available upon request"
Example 6 - Show specific text as output based on if it equals the value of a text form field (nested)

Using the IfElse() function to show the correct salutation depending on the HostSystem value of 'Gender'.

The binding is:

{{IfElse(Equals(HostSystem.Gender, "M"), "Dear sir", IfElse(Equals(HostSystem.Gender, "F"), "Dear madam", "Dear sir/madam"))}}

  Example A Example B Example C
Input HostSystem.Gender = "M" HostSystem.Gender = "F" HostSystem.Gender = ""
Condition True False>True False>False
Output "Dear sir" "Dear madam" "Dear sir/madam"

 

  Note

Instead of using a nested IfElse() binding as shown above, you can also use the Switch() function in this case (see example 2 in that article).

Example 7 - Show specific text as output based on if it not equals the value of a text form field

Using the IfElse() function together with Not() and Equals() to inject specific product information in the smart template based on the 'Subject' question (of type Text) originating from the response form. The binding is:

{{IfElse(Not(Equals(Form.Subject, "Product A")), "Description of all other products", "Description of product A")}}

  Example A Example B
Input Form.Subject = "As you can see our products are compliant" Form.Subject = "Product A"
Condition True False
Output "Description of all other products" "Description of product A"
Example 8 - Set visibility based on value of a checkbox form field

Using the IfElse() function to set the visibility of a smart field based on the value of the 'CheckboxInclude' checkbox question (of type Checkbox) originating from the response form. The binding is:

{{IfElse(Form.CheckboxInclude, VisibilityType.Visible, VisibilityType.Hidden)}}

  Example A Example B
Input Form.CheckboxInclude = "☑" Form.CheckboxInclude = "☐"
Condition True False
Output Visible text  Hidden text
Example 9 - Set visibility based on value of checkbox form fields A and C

Using the IfElse() function together with the And() to set the visibility of a smart field based on the value of two checkbox questions originating from the response form, 'Product A' and 'Product C' must both be checked for the text to be visible. The binding is:

{{IfElse(And(Form.ProductA, Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden)}}

  Example A Example B
Input

Form.ProductA = "☑"

Form.ProductC = "☑"

Form.ProductA = ""

Form.ProductC = "☑"

Condition True False
Output Visible text  Hidden text
Example 10 - Set visibility based on value of checkbox form fields A or C

Using the IfElse() function together with the Or() to set the visibility of a smart field based on the value of two checkbox questions originating from the response form, 'Product A' or 'Product C' must be checked for the text to be visible. The binding is:

{{IfElse(Or(Form.ProductA, Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden))}}

  Example A Example B
Input

Form.ProductA = ""

Form.ProductC = "☑"

Form.ProductA = ""

Form.ProductC = "☐"

Condition True False
Output Visible text  Hidden text
Example 11 - Set visibility based on value of checkbox form fields A and C (not B)

Using the IfElse() function together with And() and Not() to set the visibility of a smart field based on the value of two checkbox questions originating from the response form, 'Product A' and 'Product C' must both be checked, and 'Product B' must be unchecked for the text to be visible. The binding is:

{{IfElse(And(Form.ProductA, Not(Form.ProductB), Form.ProductC), VisibilityType.Visible, VisibilityType.Hidden))}}

  Example A Example B
Input

Form.ProductA = "☑"

Form.ProductB = "☐"

Form.ProductC = "☑"

Form.ProductA = ""

Form.ProductB = "☑"

Form.ProductC = "☑"

Condition True False
Output Visible text Hidden text
Example 12 - Set visibility based on if User Profile field contains specific value

Using the IfElse() function together with Contains() to display or hide some text based on the value of the 'UserProfile.Office.Name' originating from the User Profile.

  • If the 'UserProfile.Office.Name' is 'Copenhagen' or 'Berlin', the text should be hidden.
  • If the 'UserProfile.Office.Name' is anything else, the text should be shown.

The binding is:

{{IfElse(Contains("Copenhagen, Berlin", UserProfile.Office.Name), VisibilityType.Hidden, VisibilityType.Visible)}}

  Example A Example B Example C
Input UserProfile.Office.Name = "Berlin" UserProfile.Office.Name = "Copenhagen" UserProfile.Office.Name = "Eindhoven"
Condition True True False
Output Hidden text Hidden text Visible text
Example 13 - Dynamically insert text element or slide based on value of a checkbox form field

Using the IfElse() function to dynamically insert a text element/slide based on the value of the 'Checkbox' checkbox question (of type Checkbox) originating from the response form.
The value '123456787654321' used in the example is the Asset ID of the text element. The binding is:

{{IfElse(Form.Checkbox,"123456787654321","")}}

  Example A Example B
Input Form.Checkbox = "☑" Form.Checkbox = "☐"
Condition True False
Output Text element/slide inserted  Text element/slide not inserted

 

  Important

If the text element/slide inserted contains questions in the response form, a dropdown question should be used, as the text element/slide questions will not be displayed when using a checkbox.

Example 14 - Show value of text form field if HostSystem field is empty

Using the IfElse() function together with Equals() to display the value of the 'Subject' question (of type Text) originating from the response form if the HostSystem value is empty. The binding is:

{{IfElse(Equals(HostSystem.Subject, ""), Form.Subject, HostSystem.Subject)}}

  Example A Example B
Input HostSystem.Subject = "",
Form.Subject = "Hello world"
HostSystem.Subject = "Invitation",
Form.Subject = "Hello world"
Condition True False
Output "Hello world" "Invitation"
classification chart title tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.