How to write an expression rule that returns multiple values? Would you share an example?
Discussion posts and replies are publicly visible
Hi Cindy,
Just enclose your output within {} to return multiple values from expression rule. Find the below example
load( local!country:"India", local!city:"Nagpur", local!employees:100, { local!country, local!city, local!employees })
Just create a dictionary or a list. Looks like this
/* Dictionary */ {key1: "value1", key2: "value2"} /* List */ {1,2,3,4,5}
BTW, there is no need to create local variables and make it a "return" value
/* Not necessary */ a!localVariables( local!returnValue: {1,2,3,4,5}, local!returnValue )
Creating a dictionary on before returning. Then you can reference those values via index().
XXX_getVariableAndLocation()
with( local!tempVariable: rule!XXX_processVariable(), /*** FOR EXAMPLE: RETURNS 5 ****/ local!variableLocation: rule!XXX_getVariableExpression(), { value: local!tempVariable, location: local!variableLocation } )
Main Expression
load( local!variableDetails: rule!XXX_getVariableAndLocation(), index(local!variableDetails, "value", null) )
Output is 5.
Just an aside...in a future release of Appian you'll be able to return data in 'map' format (this is already generated from a Decision Rule if you have multiple outputs of different data types) but you'll have access to the a!map() function natively, as per the following example:
a!map( myInteger: 99, myString: "Ipsum Lorem", myDecimal: 3.14159, myBoolean: true, myIntegerList: {3,5,7,9} )
The above code returns the following output:
This will bust the current limitation where Expression Rules can only return one 'type' of data as their final result.
What will be the difference to the "{}" dictionary?
+1, curious what benefits this offers compared to essentially returning an "any type" dictionary.
That when extracting the values from the map the value won't be wrapped in a variant but will be natively of the type expected:
{ myInteger: 99, myString: "Ipsum Lorem", myDecimal: 3.14159, myBoolean: true, myIntegerList: {3,5,7,9} }.myInteger
returns:
whereas:
a!map( myInteger: 99, myString: "Ipsum Lorem", myDecimal: 3.14159, myBoolean: true, myIntegerList: {3,5,7,9} ).myInteger
According to the feature description:
"[this]...will help to reduce unexpected errors in your interfaces and rules. Maps will simplify your expressions and make it easier to work with other types."
oh, that sounds great then - i can finally get rid of this nervous twitch i've developed over time by being caught off guard by weird type mismatches
Hah, likewise! I can't promise you which release of Appian it'll arrive in but it's in progress.
Really nice little feature!