A!foreach() in Expression rules

Certified Associate Developer

I have built an expression rule where I am using a!foreach() and looping through items. Below is the query.

a!localVariables(
local!var: a!queryRecordType(
recordType: 'recordType!rec_type1,
pagingInfo: a!pagingInfo(1, 10)
),
a!forEach(
items: { local!var}.data,  //4 data items from the record type table are that it can iterate on//
expression: fv!item['recordType!rectype1.fields.col1'] [1]
)
)

The record type I am querying here has 4 items that I am iterating in using a!foreach(). Now expression gives a single output (specific cell's data from table i.e. first record's col1 value). My expectation was that this would give same table cell data as output but 4 times instead of 1 time.

Is it due to the fact that in every iteration fv!item moves to the subsequent item as current array item and therefore cannot give that cell's data as output remaining 3 times since it has already moved ahead to next item.

Let me know if I am thinking correctly or is there some other reason. (Refer: Below image shows the single output for above query.)

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Lead Developer
    in reply to ayushsrivastava

    1) There are edge cases where putting a list into a list, just results in a list with one item that is that list. In other cases, Appian merges the items into just a flat list. While typically not a problem, this can lead to painful bugs almost impossible to debug.

    The result is a list of variant that contains another list that has 10 items. Now the foreach iterates on only a single item. Your way of indexing returns the first item in the list only.

    Fixed:

    2) fv!item is always the iteration's item. It is just a matter of what are the items in that list.

Children
  • 0
    Certified Associate Developer
    in reply to Stefan Helzle

    Hi Stefan,

    Can you check and help me understand that why in below case we see "list of variant" instead of "List of GL report items" directly? Since if I had just queried GL report below then it would have given me a list of map or for data only it would have said "List of TA GL Report - n items". (Not sure if it is due to the filter condition which is using another record type.)

  • It's because you have a list of lists, and a!forEach() doesn't automatically flatten it into a single list.

    I think it's also important to note that we have been talking about some scenarios that result in a list of lists or indexing into various items, but I strongly recommend that you don't actually do the expression you specified above (where you have a query inside of a!forEach()). In that expression, you're iterating over a list and running a query for every item in the list, and if you aren't careful, this could result in a huge number of queries that get executed.

    In general you should refactor scenarios like this to run a single query and then split up or manipulate the data after the query is executed. I think part of the strangeness of your result set stems from the fact that it would be unlikely to get into this scenario assuming you are doing a single query.