Hello,
I have a similar question to: https://community.appian.com/discussions/f/rules/21530/getting-one-property-value-from-dictionary-based-upon-another-property-value.
But instead of returning true/false, I need it to match it against a string value on match condition:
a!localVariables( local!myDictionary: { { item: 1, active: "A", description: "Widget", color: "Green"}, { item: 2, active:"B", description: "another", color: "red"} }, local!tempResults: if(fn!index(local!myDictionary,"active","") = "A" ,"yes","no"), reject(fn!isnull, local!tempResults) )
The index returns the "A", but when I do a condition against it (if statement), nothing returns. I want the string "Green" returned if it matches "A". IT says it doesn't match it.
Discussion posts and replies are publicly visible
Hi, the fn!index() returns an array. so we can use contains() operator instead of =
The way I'd approach this is to cycle through the local!myDictionary variable checking for active = "A" and if it does return the color index. See code below.
There are many ways to achieve a similar outcome though so depending on your exact use case a different approach may be better/more efficient. This is just one way.
a!localVariables( local!myDictionary: { { item: 1, active: "A", description: "Widget", color: "Green" }, { item: 2, active: "B", description: "another", color: "red" } }, a!forEach( items: local!myDictionary, expression: if( exact( tostring( index( fv!item, "active", {} ) ), "A" ), index( fv!item, "color", {} ), {} ) ) )
Hi there,
This can be effectively achieved using displayValue function(). Try the below piece of code.
a!localVariables( local!myDictionary: { { item: 1, active: "A", description: "Widget", color: "Green"}, { item: 2, active:"B", description: "another", color: "red"} }, displayvalue( "A", local!myDictionary.active, local!myDictionary.color, "" ) )
I really like that! I've never used displayvalue() but it's clearly helpful.
It does appear to only work for the first match though so I've come up with the following. Is there a better way if the use case requires more than one match?
a!localVariables( local!myDictionary: { { item: 1, active: "A", description: "Widget", color: "Green" }, { item: 2, active: "B", description: "Another", color: "red" }, { item: 3, active: "A", description: "Blah", color: "Blue" } }, reject( fn!isnull, a!forEach( items: local!myDictionary, expression: displayvalue( "A", {fv!item.active}, fv!item.color, null ) ) ) )
While people seem to love solving any challenge using a!foreach, most of the time this is not necessary.
a!localVariables( local!myDictionary: { { item: 1, active: "A", description: "Widget", color: "Green" }, { item: 2, active: "B", description: "Another", color: "red" }, { item: 3, active: "A", description: "Blah", color: "Blue" } }, index( local!myDictionary.color, wherecontains("A", touniformstring(local!myDictionary.active)), {} ) )
do you mean :
local!tempResults: if(contains(fn!index(local!myDictionary,"active",""),"A") ,"yes","no"),
This works, but I figured there would be a simpler approach with abuilt-in function. But this is acceptable. Thank you.
This works as well. The built-in function is what I figured I needed, I just didn't know which one. Thank you.
Yes that's a great observation @ajhick, I do have a dictionary where there are two active with "A". If inserting to the DB, you would need just one and not both. How would you go about just choosing one?
Yes I didnt want to utilize the a!forEach as it may be overkill in a process model where thousands of records are being read unless it was the only option. This is the better answer so far.