FunctionCopy link to clipboard
if( condition, valueIfTrue, valueIfFalse )
Returns valueIfTrue
if condition
returns true
; returns valueIfFalse
otherwise.
See also:
- a!match(): Allows specifying a series of condition pairs using equal and then, or whenTrue and then. Can often achieve more complex logic than
if()
. - choose(): Returns a result based on the index provided. Can often result in similar logic to
if()
when paired withwherecontains()
. - a!defaultValue: Consider using the
a!defaultValue
function when you are replacing a null value with another value or have nestedif
andisnull
statements.
ParametersCopy link to clipboard
Keyword | Type | Description |
---|---|---|
|
Boolean |
A test that determines whether valueIfTrue or valueIfFalse will be returned. |
|
Any Type |
The value to be returned if condition returns true. |
|
Any Type |
The value to be returned if condition returns false. |
ReturnsCopy link to clipboard
Any Type
Usage considerationsCopy link to clipboard
Evaluation propertiesCopy link to clipboard
- Unlike most functions,
if()
does not always evaluate all of its parameters. When passed a single condition, the function evaluates the condition and then only the value parameter that is returned. - All parameters are required for the
if()
function. - The condition value is cast to a Boolean. If it fails to cast, the
if()
function returns an error.
Null and empty valuesCopy link to clipboard
- When null values are passed to the condition parameter, they are considered to be false.
- When null values are passed to the
valueIfTrue
orvalueIfFalse
parameters, a null value is also returned for the corresponding true or false.
Lists of valuesCopy link to clipboard
When a list is passed to the condition parameter, it is treated as a list of conditions, so the valueIfTrue
and valueIfFalse
parameters return lists of values. This means that when passed an empty list, if()
always returns an empty list.
If the lists in the condition
and value parameters are the same length, the index of each condition corresponds to the item selected for the result. For example:
1
2
3
4
5
if(
{true, false, true, false, true},
{"One", "Two", "Three", "Four", "Five"},
{"A", "B", "C", "D", "E"}
)
Copy
Returns {"One", "B", "Three", "D", "Five"}
.
However, if the value parameters have shorter lists than the condition, the index is not maintained. Instead, each item is returned in order (and repeats if necessary). For example:
1
2
3
4
5
if(
{true, false, true, false, true, true, true},
{"One", "Two", "Three", "Four"},
{"A", "B", "C", "D"}
)
Copy
Returns {"One", "A", "Two", "B", "Three", "Four", "One"}
.
When passed a list containing a single boolean and lists in the value parameters, only the first item of the selected list will be returned. However, if a single value is passed to the condition and there are lists in the value parameters, the entire list is returned.
ExamplesCopy link to clipboard
Basic examplesCopy link to clipboard
if(null, 1, 0)
returns 0
if(true, 1, error("Doesn't get evaluated"))
returns 1
(because the parameter with error()
does not get evaluated)
if(true, { 2, 4, 6 }, { 1, 3, 5 })
returns { 2, 4, 6 }
if({true, false, true}, { 2, 4, 6 }, { 1, 3, 5 })
returns { 2, 3, 6 }
if({true}, { 2, 4, 6 }, { 1, 3, 5 })
returns { 2 }
if({}, { 2, 4, 6}, { 1, 3, 5 })
returns {}
Evaluating only one outputCopy link to clipboard
if(true, 1, error("Doesn't get evaluated"))
returns 1
(because the parameter with error()
does not get evaluated)
Using if() to return a default resultCopy link to clipboard
The if()
function is often used when calling an integration to handle cases where an integration failed or returned a null response.
1
2
3
4
5
6
7
8
a!localVariables(
local!result: rule!MY_Integration(),
if(
a!isNullOrEmpty(local!result),
0,
local!result.totalCount
)
)
Copy
In this case, when the integration returns an empty result, a default count of 0 is returned. Otherwise, the total count from the result is used.
Using if() to determine whether to show a component as read onlyCopy link to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
a!localVariables(
local!isReadOnly: true,
local!value,
a!textField(
label: "Text",
labelPosition: if(
local!isReadOnly,
"JUSTIFIED",
"ABOVE"
),
readOnly: local!isReadOnly,
required: if(
local!isReadOnly,
false,
true
),
saveInto: local!value,
value: local!value,
)
)
Copy
When the variable local!isReadOnly
is changed, it determines the component's readOnly
value, as well as additional display properties.
Feature compatibilityCopy link to clipboard
The table below lists this function's compatibility with various features in Appian.
Feature | Compatibility | Note |
---|---|---|
Portals | Compatible | |
Offline Mobile | Compatible | |
Sync-Time Custom Record Fields | Compatible | Can be used to create a custom record field that only evaluates at sync time. |
Real-Time Custom Record Fields | Incompatible | Custom record fields that evaluate in real time must be configured using one or more Custom Field functions. |
Process Reports | Compatible | |
Process Events | Compatible |