Invalid index (4) for list: valid range is 1...3

I have created a rule to calculate the number of hours from an array which is passed to the rule as a rule input.  This array can increase but is not refreshing in the calling rule and the following message is being received.

Below is the code for the rule calling the array:

load(
  if(
    rule!CMN_checkNullOrEmpty(ri!casualWorkSchedule),
    tostring({}),
    with(
      local!weeks: index(ri!casualWorkSchedule, "weeks", {}),
      local!weekEndingDate: (todate(local!weeks) + 4),
      local!totalHours: a!forEach(
        items: rule!CAT_getUniqueElements(local!weekEndingDate),
        expression: rule!CAT_getTotalHoursWorkedByFanWeekEndingDate(
          fan: ri!selectedCasual,
          weekEndingDate: fv!item
        )
      ),
      local!proposedHours: a!flatten(
        a!forEach(
          items: ri!casualWorkSchedule,
          expression: 'type!{urn:com:appian:types}CAT_ACADEMIC_WORKLOAD'(
            week: index(local!weeks, fv!index, null),
            totalHours: {
              tointeger(
                rule!CAT_calculateProposedHoursFromCasualWorkSchedule(tempWorkSchedule: fv!item)
              )
            } + { tointeger(property(local!totalHours,"totalHours",null)[fv!index]) }
          )
        )
      ),
      if(
        and(
          rule!CMN_checkNullOrEmpty(local!weekEndingDate),
          rule!CMN_checkNullOrEmpty(local!totalHours),
          rule!CMN_checkNullOrEmpty(local!proposedHours)
        ),
        {},
        {
          a!sectionLayout(
            label: "",
            contents: {
              a!forEach(
                items: local!proposedHours,
                expression: {
                  if(
                    fv!item.totalHours > cons!CAT_VALID_WORK_HOURS,
                    a!richTextDisplayField(
                      labelPosition: "COLLAPSED",
                      value: {
                        a!richTextItem(
                          text: "This casual will be working " & fv!item.totalHours & " hours for the week of " & fv!item.week & " across this and/or other topics.  Please consult with the topic or course coordinator or Dean (P&R) before submitting.  Refer to the Casual Workload report.",
                          color: "#F06A28"
                        )
                      }
                    ),
                    {}
                  )
                }
              )
            }
          )
        }
      )
    )
  )
)

Can someone please suggest changes to fix the Line 24 error?

Thanks

Irene Best

  Discussion posts and replies are publicly visible