I'm trying to implement an expression rule that iterates over a list and returns a filtered list, and struggling to figure out how to do this. It seems like filter() would be the way to go, but I don't see any examples of what the context parameter is supposed to look like. What I want to do is something like:
rule input: fromDate (a Date)
rule:
filter(
rule!shouldItemBeIncluded(),
rule!getItemList(),
context: /* I'm lost here - I need to pass two parameters to shouldItemBeIncluded(), the item, and the fromDate */
)
How do I pass those parameters to the supplied predicate?
Discussion posts and replies are publicly visible
It's recommended to use the forEach function instead of filter() - https://docs.appian.com/suite/help/19.1/fnc_looping_a_foreach.html - for this kind of use case.
For filter() itself, context is option so you don't need to include any thing for that third parameter.
Doesn't that only work if the resulting array should be the same size as the array being iterated over?
That's a good point, I was thinking of looping functions more generally. I also realized I didn't read your post correctly. Can you try the following, where pv!fromDate = whatever your fromDate value is?
rule!shouldItemBeIncluded(_, pv!fromDate),
rule!getItemList()
Here's the rule:
filter( rule!OTM_ShouldTaskBeCreated(_, fromDate), rule!OTM_GetTaskDefinitions())
here's the error:
Expression evaluation error in rule 'otm_shouldtaskbecreated': The load() function cannot be used within rules called by looping functions other than a!forEach(). Pass the necessary variables via rule inputs or use a!forEach().
(because those rules do have load() to define temporary variables)
also had tried:
filter( rule!OTM_ShouldTaskBeCreated(), rule!OTM_GetTaskDefinitions(), fromDate)
(rule OTM_ShouldTaskBeCreated() takes two parameters: the CDT that OTM_GetTasDefinitions() returns a list of, and a date)
error:
Expression evaluation error at function rule!OTM_ShouldTaskBeCreated [line 2]: Rule 'otm_shouldtaskbecreated' has 2 parameters, but instead passed 0 parameters.
You should be able to just change loads() to withs() within your OTM_ShouldTaskBeCreated rule to get it to work for this example:
filter(rule!OTM_ShouldTaskBeCreated(_, fromDate),rule!OTM_GetTaskDefinitions())
Was able to get rid of the load() in the OTM_ShouldTaskBeCreated() rule, and your suggestion worked! Is that _ thing documented anywhere?
Surprisingly, I don't see it documented and I'm not aware of Appian deprecating it. I've always called it a "deferred" parameter, and basically it just acts as a placeholder for each item in your list.
I did a few quick tests, and you might be able to avoid its use with this altered version of your second example:
filter(rule!OTM_ShouldTaskBeCreated, <---I just removed the parens for this initial function referencerule!OTM_GetTaskDefinitions(),fromDate)
Yup that works too - thanks for the help!
The docs are buried a bit on it: https://docs.appian.com/suite/help/19.1/Expressions.html#advanced-evaluation