Skip to main content
April 20, 2023
Question

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

  • April 20, 2023
  • 9 replies
  • 0 views

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

    9 replies

    stefanhelzle0001
    April 20, 2023

    What is this code meant to do?

    a!forEach(
            items: remove(
              rule!APN_distinct(local!isLoggedInUserGLL),
              length(
                rule!APN_distinct(local!isLoggedInUserGLL)
              )
            ),
            expression: split(rule!APN_displayUser(fv!item), ";")
          ),

    mikes0011
    Brainy
    April 20, 2023

    That entire local variable definition is a nightmare of spaghetti code ... edit: i understand what it's supposed to do now, at least.  It's meant to "remove" the last item in the array (when the array is greater than 1) only for the sake of displaying it after " and "...

    I've posted a much cleaner way of doing this below, served directly by a!forEach() and its inherent functionality.

    anshurajsinghr3189
    April 20, 2023

    If there is only one value in your local storage, and you use the remove function, it will remove the last value. Therefore, please ensure that there are more than one values in your local storage before using the remove function.

    mikes0011
    Brainy
    April 20, 2023

    Actually I suspect the error in the original post may have resulted in an array of size 0.  The original code already checks for an array of size 1, but if the list had 0 people in it, it would try to "remove" an entry from an empty list.

    mikes0011
    Brainy
    April 20, 2023

    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.

    mikes0011
    Brainy
    April 20, 2023

    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
        )
      )
    )

    April 20, 2023

    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.