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

  • : Would you mind explaining what you did to fix this error? I'm hitting something similar at the moment. Thanks!

  • The invalid index error is generated with square-bracket indexing such as ri!myVar[15], when the index requested is higher than the count of items in the variable.  Feel free to start a new thread with your details and we'll be happy to take a look.  

    In line 24 above:

     } + { tointeger(property(local!totalHours,"totalHours",null)[fv!index]) }

    This error would be thrown due to "[fv!index]" if the local!totalHours variable has less items than the ri!casualWorkSchedule variable.  Changing the line to use fn!index() as below would resolve the error message.

    } + { tointeger(index(property(local!totalHours,"totalHours",null),fv!index,null)) }

  • Hi Robin

    I don't really remember what I did to fix this but below is the code that works:

    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: rule!CAT_calculateProposedHoursFromCasualWorkSchedule(tempWorkSchedule: fv!item)
              )
            )
          ),
          local!sumHours: a!flatten(
            a!forEach(
              items: ri!casualWorkSchedule,
              expression: 'type!{urn:com:appian:types}CAT_ACADEMIC_WORKLOAD'(
                week: index(local!weeks, fv!index, null),
                totalHours: sum(
                  {
                    index(
                      {
                        todecimal(
                          property(local!proposedHours, "totalHours", null)
                        )
                      },
                      fv!index,
                      null
                    ),
                    index(
                      {
                        todecimal(
                          property(local!totalHours, "totalHours", null)
                        )
                      },
                      fv!index,
                      null
                    ),

                  }
                )
              )
            )
          ),
          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!sumHours,
                    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"
                            )
                          }
                        ),
                        {}
                      )
                    }
                  )
                }
              )
            }
          )
        )
      )
    )

    Hope this helps.

    Thanks

    Irene