Skip to main content
December 6, 2023
Solved

working with List of Maps

  • December 6, 2023
  • 5 replies
  • 2 views

Hi!

I'm having trouble trying to modify this list of maps (which I get from a!queryRecordType with an aggregation).

local!Grades: {
a!map(Group: "A", Excellent: 0, Good: 0, Regular: 0),
a!map(Group: "A", Excellent: 2, Good: 0, Regular: 0),
a!map(Group: "B", Excellent: 0, Good: 0, Regular: 2),
a!map(Group: "B", Excellent: 0, Good: 2, Regular: 0),
a!map(Group: "C", Excellent: 0, Good: 2, Regular: 0),
},

The aggregation does not allow me to get each Group in only 1 row, because I need to "count" the grades by category (Excellent, Good, Regular)

I would like to display that result in a read-only grid and am trying to prepare the data before displaying it there (as I read in some other suggestions).

The result I am trying to obtain is this:

local!Grades: {
a!map(Group: "A", Excellent: 2, Good: 0, Regular: 0),
a!map(Group: "B", Excellent: 0, Good: 2, Regular: 2),
a!map(Group: "C", Excellent: 0, Good: 2, Regular: 0),
},

In other words, I want to match each row of the grid to each group, and the columns will be: "Excellent", "Good", "Regular".

I tried using 2 nested foreach... but it doesn't work.

Please, help me with some ideas!

Thank you in advance!

Best answer by mikes0011

It doesn't even really require a nested ForEach or (if you'd rather avoid the legacy function) reduce(), just a bit of reassembly using a!forEach().

a!localVariables(
  local!Grades: {
    a!map(Group: "A", Excellent: 0, Good: 0, Regular: 0),
    a!map(Group: "A", Excellent: 2, Good: 0, Regular: 0),
    a!map(Group: "B", Excellent: 0, Good: 0, Regular: 2),
    a!map(Group: "B", Excellent: 0, Good: 2, Regular: 0),
    a!map(Group: "C", Excellent: 0, Good: 2, Regular: 0),
  },
  
  local!uniqueGroups: rule!RULE_General_distinct(    
        /* simple helper rule that unions an array against itself */
    value: local!Grades.Group
  ),
  
  a!forEach(
    local!uniqueGroups,
    
    a!localVariables(
      local!listForCurrentGroup: index(
        local!Grades,
        wherecontains(fv!item, local!Grades.Group),
        {}
      ),
      a!map(
        Group: fv!item,
        ExcellentSum: sum(local!listForCurrentGroup.Excellent),
        GoodSum: sum(local!listForCurrentGroup.Good),
        RegularSum: sum(local!listForCurrentGroup.Regular)
      )
    )
  )
)

5 replies

prakhars0731
December 6, 2023

You can use group by group or try using where contains function.

December 6, 2023

Thank you for your replied!

stefanhelzle0001
Brainy
December 6, 2023

You could use the reduce() function to iterate on the input data and create a output map. Some ideas here: https://appian.rocks/2022/08/29/complex-algorithms-in-appian/

Below some example code that picks unique random values from a list. This pattern allows you to put all the code into a single expression. This code does not implement a solution to your use case!

if(
  ri!isRecursion,
  a!localVariables(
    local!index: ceiling(rand() * count(ri!data.chars)),
    a!map(
      chars: remove(ri!data.chars, local!index),
      output: append(ri!data.output, ri!data.chars[local!index])
    )
  ),
  reduce(
    rule!SSH_RandomPicker(
      isRecursion: true,
      data:_,
      num:_
    ),
    a!map(
      chars: char(65 + enumerate(26)),
      output: {}
    ),
    enumerate(10)
  )
)

December 6, 2023

Thank you for your replied!

mikes0011
mikes0011Answer
Brainy
December 6, 2023

It doesn't even really require a nested ForEach or (if you'd rather avoid the legacy function) reduce(), just a bit of reassembly using a!forEach().

a!localVariables(
  local!Grades: {
    a!map(Group: "A", Excellent: 0, Good: 0, Regular: 0),
    a!map(Group: "A", Excellent: 2, Good: 0, Regular: 0),
    a!map(Group: "B", Excellent: 0, Good: 0, Regular: 2),
    a!map(Group: "B", Excellent: 0, Good: 2, Regular: 0),
    a!map(Group: "C", Excellent: 0, Good: 2, Regular: 0),
  },
  
  local!uniqueGroups: rule!RULE_General_distinct(    
        /* simple helper rule that unions an array against itself */
    value: local!Grades.Group
  ),
  
  a!forEach(
    local!uniqueGroups,
    
    a!localVariables(
      local!listForCurrentGroup: index(
        local!Grades,
        wherecontains(fv!item, local!Grades.Group),
        {}
      ),
      a!map(
        Group: fv!item,
        ExcellentSum: sum(local!listForCurrentGroup.Excellent),
        GoodSum: sum(local!listForCurrentGroup.Good),
        RegularSum: sum(local!listForCurrentGroup.Regular)
      )
    )
  )
)