Invalid index (0) for list: valid range is empty

Certified Associate Developer

Hi ,

I am getting below error the attached code when there I don't pass value for ri!gLLs. Can someone please help in resolving this.

"Expression evaluation error at function 'remove' [line 67]: Invalid index (0) for list: valid range is empty"

a!localVariables(
  /* Appending logged in user with GLLS from LUContacts if logged in user is GLL */
  local!isLoggedInUserGLL: rule!APN_distinct(
    if(
      rule!APN_isEmpty(ri!gLLs),
      {},
      if(
        and(
          not(
            contains(
              touniformstring(
                index(
                  a!groupMembers(
                    group: cons!RGRACSLBL_GROUP_AGLL,
                    direct: true(),
                    memberType: "USER"
                  ),
                  "data",
                  {}
                )
              ),
              touniformstring(ri!loggedInUser)
            )
          ),
          or(
            contains(
              touniformstring(
                index(
                  a!groupMembers(
                    group: cons!RGRACSLBL_GROUP_EU_LL_USERS,
                    direct: true(),
                    memberType: "USER"
                  ),
                  "data",
                  {}
                )
              ),
              touniformstring(ri!loggedInUser)
            ),
            contains(
              touniformstring(
                index(
                  a!groupMembers(
                    group: cons!RGRACSLBL_GROUP_WPL,
                    direct: true(),
                    memberType: "USER"
                  ),
                  "data",
                  {}
                )
              ),
              touniformstring(ri!loggedInUser)
            )
          )
        ),
        append(ri!gLLs, ri!loggedInUser),
        ri!gLLs
      )
    )
  ),
  /* Formatting GLLs text */
  local!glls: if(
    length(local!isLoggedInUserGLL) = 1,
    rule!APN_displayUser(local!isLoggedInUserGLL),
    joinarray(
      a!forEach(
        items: remove(
          rule!APN_distinct(local!isLoggedInUserGLL),
          length(
            rule!APN_distinct(local!isLoggedInUserGLL)
          )
        ),
        expression: split(rule!APN_displayUser(fv!item), ";")
      ),
      ", "
    ) & " and " & rule!APN_displayUser(
      difference(
        rule!APN_distinct(local!isLoggedInUserGLL),
        remove(
          rule!APN_distinct(local!isLoggedInUserGLL),
          length(
            rule!APN_distinct(local!isLoggedInUserGLL)
          )
        )
      )
    )
  ),
  if(
    contains(
      /* CCN */
      {
        cons!RGRACSLBL_NOTIFICATION_TYPES[1],
        cons!RGRACSLBL_NOTIFICATION_TYPES[16]
      },
      ri!notificationType
    ),
    joinarray(
      insert(
        split(
          "Please contact[GLL]directly for any questions regarding this CCN.",
          "[GLL]"
        ),
        local!glls,
        2
      ),
      " "
    ),
    if(
      contains(
        /* Pre-sMA, Revised Pre-sMA */
        {
          cons!RGRACSLBL_NOTIFICATION_TYPES[7],
          cons!RGRACSLBL_NOTIFICATION_TYPES[25]
        },
        ri!notificationType
      ),
      substitute(
        cons!RGRACSLBL_PRE_SMA_AGLL_RICH_TEXT_CONSTANTS[7],
        "[GLL]",
        local!gLLs
      ) & " " & cons!RGRACSLBL_PRE_SMA_AGLL_RICH_TEXT_CONSTANTS[8],
      if(
        contains(
          /* Core SLU */
          {
            cons!RGRACSLBL_NOTIFICATION_TYPES[18],
            cons!RGRACSLBL_NOTIFICATION_TYPES[6]
          },
          ri!notificationType
        ),
        "Please contact " & local!glls & " directly for any questions regarding this update.",
        if(
          ri!notificationType = cons!RGRACSLBL_HQ_INITIATED_NON_SAFETY_LABEL_UPDATE_NOTIFICATION_TYPE,
          /* Non-Safety */
          joinarray(
            insert(
              split(
                cons!RGRACSLBL_TEXT_HQ_NONSAFETY_FREETEXT,
                "[GLL]"
              ),
              local!glls,
              2
            ),
            " "
          ),
          if(
            contains(
              /* Advisory */
              {
                cons!RGRACSLBL_NOTIFICATION_TYPES[9],
                cons!RGRACSLBL_NOTIFICATION_TYPES[17]
              },
              ri!notificationType
            ),
            joinarray(
              insert(
                split(
                  "Please contact[GLL]directly for any questions regarding this Advisory.",
                  "[GLL]"
                ),
                local!glls,
                2
              ),
              " "
            ),
            {}
          )
        )
      )
    )
  )
)

  Discussion posts and replies are publicly visible

Parents
  • Instead of this impossible mesh of spaghetti including questionable uses of remove(), split(), etc, which I suspect is only being used to format the list like "Name 1, Name 2 and Name 3", I suggest you actually utilize the functionality found in a!forEach to do that work for you.  The usage pattern I suggest is as follows.  Note that you'd merely need to add back in your "displayUser" function here around "fv!item".

    a!localVariables(
      
      local!loggedInUsers: {"user1", "user2", "user3"},
      
      concat(
        a!forEach(
          local!loggedInUsers,
          if(
            fv!isFirst,
            "",
            if(
              fv!isLast,
              " and ",
              ", "
            )
          ) & fv!item
        )
      )
    )

    Note that this will work both for multi-member lists as well as one-member lists, inherently, without needing to resort to such messy tactics as found in the original code.

  • Additionally I'd suggest that if a list of distinct users is necessary to use here, then instead of calling APN_distinct() over and over again, simply create the list of distinct users in a local variable once and use that.

    Instead:

    a!localVariables(
      
      local!loggedInUsers: {"user1", "user2", "user1", "user3"},
      
      /* use Distinct function ONCE */
      local!distinctUsers: rule!APN_distinct(local!loggedInUsers),
      
      concat(
        a!forEach(
          local!distinctUsers,
          if(
            fv!isFirst,
            "",
            if(
              fv!isLast,
              " and ",
              ", "
            )
          ) & fv!item
        )
      )
    )

  • 0
    Certified Associate Developer
    in reply to Mike Schmitt

    Hi Mike,

    Thanks you for the detailed explanation. Actually I tried to analyze the code which was written by someone. Since I couldn't figure out what is happening here, posted it in the community.

  • Yes, I suspected it may have been inherited code - I've inherited similarly impossible-looking code plenty of times before.  Please try the approach I outlined above and let me know if it seems to work for you.

Reply Children
No Data