Articles in this section

How to compare one value to multiple values and return results accordingly using Switch()

This article explains what the Switch() 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 Switch() function? 

Switch() is a function that compares one value to multiple values and returns results accordingly, if nothing matches the conditions set in the function, a default value will be inserted. It can also be seen as slightly similar to a nested IfElse() function. 

Switch() logic

Syntax {{Switch(ValueToCompare, Value1, Result1, Value2, Result2..., DefaultValue)}}
Input ValueToCompare, Value1, Value2..., DefaultValue
Output Value1 -> Result1, Value2 -> Result2, Else -> DefaultValue

Switch() function examples

Example 1

Using the Switch() function with the "Sensitivity" dropdown question originating from the response form to adjust the text displayed in the template. The binding is:

{{Switch(Form.Sensitivity.Name,"Top Secret","Strictly Confidential","Internal","Internal Only","Public")}}

  Example A Example B Example C
Input Form.Sensitivity = "Top Secret" Form.Sensitivity = "Internal" Form.Sensitivity = Not "Internal" or "Top Secret"
Output "Strictly Confidential" "Internal Only" "Public"

Example 2

Using the Switch() function to show the correct salutation depending on the HostSystem value of 'Gender'. The binding is:

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

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

Example 3

Using the Switch() function with the 'Company' originating from the User Profile to translate a numerical value into a company name. The binding is:

{{Switch(UserProfile.Company,"1","Templafy ApS","2","iWriter","No Company")}}

  Example A Example B Example C
Input UserProfile.Company = "1" UserProfile.Company = "2" UserProfile.Company = Not "1" or "2"
Output "Templafy ApS" "iWriter" "No Company"

Example 4

Instead of plain text you can also use other form fields in the binding, e.g. Form.Subject. The value of Form.Subject is "Meeting". The binding is:

{{Switch(Form.Choice,"1", "Hello world", "2", Form.Subject, "No Choice")}}

  Example A Example B Example C
Input Form.Choice = "1" Form.Choice = "2" Form.Choice = Not "1" or "2"
Output "Hello world" "Meeting" "No Choice"

Example 5

Using the Switch() function together with the StringJoin() function to display specific text depending if both questions 1 and 2 are empty. The binding is:

{{Switch(StringJoin("", Form.Question1, Form.Question2),"", "Question 1 and 2 are empty", "Question 1 and/or 2 contain(s) a value")}}

  Example A Example B
Input

Form.Question1 = ""

Form.Question2 = ""

Form.Question1 = ""

Form.Question2 = "Hello world"

Output "Question 1 and 2 are empty" "Question 1 and/or 2 contain(s) a value"

Example 6

In this example this 'Offices' data source is used:

OfficesDataSource.png

Using the Switch() function together with the 'DocumentLanguage' originating from the User Profile to display the phone number in different formats. Depending on the DocumentLanguage set in the user profile the phone number is shown in a different format. If the DocumentLanguage is not "nl-NL" or not set then the default value is returned. The binding is:

{{Switch(DocumentLanguage,"nl-NL", UserProfile.Office.Phone_NL, UserProfile.Office.Phone_EN)}}

  Example A Example B Example C
Input DocumentLanguage = "nl-NL" DocumentLanguage = "en-US" DocumentLanguage = not set
Output "040 - 1234 567" "+31 (0)40 1234 567" "+31 (0)40 1234 567"
classification chart title tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.