Conditional Statements
Conditional statements (or if/else statments) are represented using
<dcs.If>
,
<dcs.Else>
,
<dcs.ElseIf>
blocks.
If/Else
The below code block will check the Boolean value of
TestVar1
and output a string stating the Boolean value in plaintext.
<dcs.Var Name="TestVar1" Type="Boolean" SetTo="false" />
<dcs.If Target="$TestVar1">
<dc.Out>TestVar1 is true</dc.Out>
</dcs.If>
<dcs.Else>
<dc.Out>TestVar1 is false</dc.Out>
</dcs.Else>
Output:
TestVar1 is false
If/ElseIf/Else
The below code uses the standard logic convention of nested if/elseif conditional checks to see if
TestVar1
is less than 8, exactly 8 or greater than 8.
<dcs.Var Name="TestVar1" Type="Integer" SetTo="8" />
<dcs.If Target="$TestVar1" LessThan="8">
<dc.Out>TestVar1 is less than 8</dc.Out>
</dcs.If>
<dcs.ElseIf Target="$TestVar1" Equal="8">
<dc.Out>TestVar1 is 8</dc.Out>
</dcs.ElseIf>
<dcs.Else>
<dc.Out>TestVar1 is greater than 8</dc.Out>
</dcs.Else>
Output:
TestVar1 is 8
Switch statements
NOTE: dcs.Switch is currently flawed with the fact that it will run each matching Case rather than only the first matching Case. This will be fixed in future versions.
Switch statements compare one or more variables to several case expressions and return the first case that is found to be true.
In the below example, the previous If/ElseIf example is rewritten using Switch syntax.
<dcs.Var Name="TestVar1" Type="Integer" SetTo="8" />
<dcs.Switch Target="$TestVar1">
<dcs.Case LessThan="8">
<dc.Out>TestVar1 is less than 8</dc.Out>
</dcs.Case>
<dcs.Case Equal="8">
<dc.Out>TestVar1 is 8</dc.Out>
</dcs.Case>
<dcs.Case GreaterThan="8">
<dc.Out>TestVar1 is greater than 8</dc.Out>
</dcs.Case>
</dcs.Switch>
Output:
TestVar1 is 8
The following Switch code block checks two variables for a variety of expressions.
<dcs.Var Name="TestVar1" Type="Integer" SetTo="8" />
<dcs.Var Name="TestVar2" Type="Integer" SetTo="3" />
<dcs.Switch>
<dcs.Case Target="$TestVar1" LessThan="8">
<dc.Out>TestVar1 is less than 8</dc.Out>
</dcs.Case>
<dcs.Case Target="$TestVar2" Equal="3">
<dc.Out>TestVar2 is 3</dc.Out>
</dcs.Case>
<dcs.Case Target="$TestVar1" GreaterThan="8">
<dc.Out>TestVar1 is greater than 8</dc.Out>
</dcs.Case>
</dcs.Switch>
Output:
TestVar2 is 3
And/Or Logic
While it is possible to have several nested or tiered if/else statments, you can also write out complex logic using
And
,
Or
and
Is
tags combined with the
If
and
Else
tags.
A block of
And/Or
tags will augment the
If/Else
tag above it. For example, the example code below:
<dcs.Var Name="TestVar1" Type="String" SetTo="Query" />
<dcs.Var Name="TestVar2" Type="String" SetTo="000" />
<dcs.If Target="$TestVar1">
<And>
<Is StartsWith="Q" />
<Is Target="$TestVar2" Equal="000" />
</And>
<dc.Out>Top If is true</dc.Out>
</dcs.If>
<dcs.Else>
<dc.Out>Top If is NOT true</dc.Out>
</dcs.Else>
if written in plain text, would read something like:
If TestVar1 begins with the letter "q" and TestVar2 is equal to "000" then return true. Otherwise, return false.
And the output for this example is:
Top If is true
In the above example, note that the first
Is
block is inheriting the
Target
from the
If
block. This can inherit down to other
Is
blocks as well or you can assign other
Target
s for each
Is
block.
Here's another example using the
Or
tags and combining them with the
Not
attribute:
<dcs.Var Name="TestVar2" Type="String" SetTo="000" />
<dcs.Var Name="TestVar3" Type="String" SetTo="001" />
<dcs.If Target="$TestVar2">
<Or>
<Is Equal="010" Not="true"/>
<Is Target="$TestVar3" Equal="100" />
</Or>
<dc.Out>Top If is true</dc.Out>
</dcs.If>
<dcs.Else>
<dc.Out>Top If is NOT true</dc.Out>
</dcs.Else>
Output: (because the first is block is true (TestVar2 is NOT equal to 010), the entire Or is true, even though the second
Is
block is false)
Top If is true
The
And
and
Or
tags can be nested inside each other, allowing for very detailed logic inside a single
If
statment:
<dcs.Var Name="TestVar1" Type="String" SetTo="Query" />
<dcs.Var Name="TestVar2" Type="String" SetTo="000" />
<dcs.Var Name="TestVar3" Type="String" SetTo="001" />
<dcs.If Target="$TestVar1">
<And>
<Or>
<Is EndsWith="y" />
<Is Target="$TestVar2" Equal="100" />
</Or>
<Or>
<Is Target="$TestVar3" Equal="001" />
<Is Target="$TestVar3" Equal="002" />
</Or>
</And>
<dc.Out>Top If is true</dc.Out>
</dcs.If>
<dcs.Else>
<dc.Out>Top If is NOT true</dc.Out>
</dcs.Else>
Output: (The entire And block is true, because each of the nested Or blocks has at least one true Is tag inside them)
Top If is true
Other Logic examples
If no condition is specified, the variable will simply be checked for a Boolean value. Numbers and strings can evaluate to 'truthy' or 'falsey' values with this syntax as well.
<dcs.Var Name="TestVar1" Type="Boolean" SetTo="true" />
<dcs.If Target="$TestVar1">
<dc.Out>TestVar1 is true</dc.Out>
</dcs.If>
Output:
TestVar1 is true
This If statement uses the
IsEmpty
parameter to check if a string is empty or not.
<dcs.Var Name="TestVar1" Type="String" />
<dcs.If Target="$TestVar1" IsEmpty="true">
<dc.Out>TestVar1 is empty</dc.Out>
</dcs.If>
Output:
TestVar1 is empty
A List with no items added to it will return True if it's
IsEmpty
parameter is checked.
<dcs.Var Name="TestVar1" Type="List" />
<dcs.If Target="$TestVar1" IsEmpty="true">
<dc.Out>TestVar1 is empty</dc.Out>
</dcs.If>
Output:
TestVar1 is empty
Similarly, a Record with no fields assigned will be considered empty.
<dcs.Var Name="TestVar1" Type="Record" />
<dcs.If Target="$TestVar1" IsEmpty="true">
<dc.Out>TestVar1 is empty</dc.Out>
</dcs.If>
Output:
TestVar1 is empty
The parameter
Not
can be added to any expression to invert it. Note that both examples both will output the same thing.
<dcs.Var Name="TestVar1" Type="String" SetTo="hello" />
<dcs.If Target="$TestVar1" IsEmpty="false">
<dc.Out>TestVar1 is not empty</dc.Out>
</dcs.If>
<dcs.If Target="$TestVar1" IsEmpty="true" Not="true">
<dc.Out>TestVar1 is not empty</dc.Out>
</dcs.If>
Output:
TestVar1 is not empty
The parameters
GreaterThan
or
LessThan
can be used to compare values. The comparison can automatically match the data type of the target variable.
LessThanOrEqual
and
GreaterThanOrEqual
are also common conditions.
<dcs.Var Name="TestVar1" Type="LocalDate" SetTo="2022-08-01" />
<dcs.If Target="$TestVar1" GreaterThan="2022-07-25">
<dc.Out>TestVar1 is after July 25</dc.Out>
</dcs.If>
Output:
TestVar1 is after July 25
Variables can be compared directly as well.
<dcs.Var Name="TestVar1" Type="LocalDate" SetTo="2022-08-01" />
<dcs.Var Name="TestVar2" Type="LocalDate" SetTo="2022-09-01" />
<dcs.If Target="$TestVar1" LessThan="$TestVar2">
<dc.Out>TestVar1 is less than TestVar2</dc.Out>
</dcs.If>
Output:
TestVar1 is less than TestVar2
The
Equal
parameter has the same logic; here, a string and date are compared without either variable changing data type.
<dcs.Var Name="TestVar1" Type="LocalDate" SetTo="2022-08-01" />
<dcs.Var Name="TestVar2" Type="String" SetTo="2022-08-01" />
<dcs.If Target="$TestVar1" Equal="$TestVar2">
<dc.Out>TestVar1 is logically equal to TestVar2</dc.Out>
</dcs.If>
Output:
TestVar1 is logically equal to TestVar2
The
IsNull
parameter can be used on scalar variables but not composite data types. Both types can be empty however.
<dcs.Var Name="TestVar1" Type="Record">
<SetField Name="Field1" Type="LocalDate" Value="2022-08-01" />
<SetField Name="Field2" Type="String" />
<SetField Name="Field3" Type="List" />
</dcs.Var>
<dcs.If Target="$TestVar1.Field1" IsNull="false">
<dc.Out>Field1 is not Null</dc.Out>
</dcs.If>
<br />
<dcs.If Target="$TestVar1.Field2" IsNull="true">
<dc.Out>Field2 is Null (and also empty)</dc.Out>
</dcs.If>
<br />
<dcs.If Target="$TestVar1.Field3" IsNull="false">
<dc.Out>Field3 is not Null (but is empty)</dc.Out>
</dcs.If>
Output:
Field1 is not Null
Field2 is Null (and also empty)
Field3 is not Null (but is empty)