Skip to main content
June 14, 2024
Solved

Looping through records and aggregating based on record value

  • June 14, 2024
  • 3 replies
  • 0 views

I want to loop through a list of 'Course' CDT and increment the local variable named progresspercent by 25 each time I encounter a Course with status "completed". E.g. a student is enrolled in 4 courses and 3 of them have "completed" status, the progresspercent variable should be 75 in this case. Following is a snippet of my code and a picture of the output I am getting.
Can you please help me figure out how I can do this?

a!localVariables(
    local!progresspercent: 0,
    {
        local!progresspercent,
        a!forEach(
            items: rule!ID_GetCourses(ri!collegeid),
            expression: {
                local!progresspercent + if(
                    fv!item['recordType!ID course.fields.status'] = "completed",
                    25,
                    0
                )
            }
        )
    }
)

/resized-image/__size/640x480/__key/communityserver-discussions-components-files/16/Screenshot-2024_2D00_06_2D00_14-at-6.30.24_2F20_PM.png

Best answer by karumurua531442

hi [mention:c499ea11856c47f2816b8b586288927c:e9ed411860ed4f2ba0265705b8793d05]  could try/modify the below code as per your inputs and check whether this is giving the required ouput.

a!localVariables(
  local!data: {
    a!map(stdId: 1, crse: 1, status: "Complete"),
    a!map(stdId: 1, crse: 2, status: "Complete"),
    a!map(stdId: 1, crse: 3, status: "Complete"),
    a!map(stdId: 1, crse: 4, status: "Inprogress"),

  },
  local!completeData: a!forEach(
    items: local!data,
    expression: if(fv!item.status = "Complete", 1, null)
  ),
  length(reject(fn!isnull, local!completeData)) * 25
)

3 replies

stefanhelzle0001
June 15, 2024

Appian uses a functional programming language including immutable variables. Why not make that foreach return a list of either 0 or 25 and then sum it up.

karumurua531442
June 15, 2024

hi [mention:c499ea11856c47f2816b8b586288927c:e9ed411860ed4f2ba0265705b8793d05]  could try/modify the below code as per your inputs and check whether this is giving the required ouput.

a!localVariables(
  local!data: {
    a!map(stdId: 1, crse: 1, status: "Complete"),
    a!map(stdId: 1, crse: 2, status: "Complete"),
    a!map(stdId: 1, crse: 3, status: "Complete"),
    a!map(stdId: 1, crse: 4, status: "Inprogress"),

  },
  local!completeData: a!forEach(
    items: local!data,
    expression: if(fv!item.status = "Complete", 1, null)
  ),
  length(reject(fn!isnull, local!completeData)) * 25
)

June 15, 2024

You can try following the code and modify it according to your requirements and data: 

a!localVariables(
  local!data: {
    a!map(stdId: 1, crse: 1, status: "completed"),
    a!map(stdId: 1, crse: 2, status: "completed"),
    a!map(stdId: 1, crse: 3, status: "completed"),
    a!map(stdId: 1, crse: 4, status: "inprogress")
  },
  length(
    wherecontains("completed", index(local!data, "status"))
  ) * 25
)