Skip to main content
September 29, 2021
Solved

foreach update another counter

  • September 29, 2021
  • 8 replies
  • 0 views

Hi there,

I have a list of applications (n) which have to be equally distributed amongst users (n). Say there are 10 applications and 3 users where application is a CDT and I am using updatedictnary () for setting application.user : user1. Distribution should be like --> 

application 1 : user 1

application 2 : user 2

application 3 : user 3

application 4 : user 1

application 5 : user 2

application 6 : user 3

application 7 : user 1

application 8 : user 2

application 9 : user 3

application 1 : user 1

How can this be achieved using only foreach? or is there any better way to do it?  Something I tried 

local!i:1,
local!UserList : {touser("ABC"),touser("PQR"),touser("GHJ")},
local!UserCount : count(local!UserList),
a!forEach(
  items: local!LoanApplicationList,
  expression :  
    updatedictionary(
      dictionary: fv!item,
      fieldsAndValues: {
        ApplicationUser :
        if(local!i=local!ListTotalUserCount,
          {
            local!i:sum(local!i,1),
           tostring(ri!UserList[local!i])            
          },
          tostring(ri!UserList[local!i])            
          )
      }
    )
)

Thanks in advance,

Sweta

Best answer by hrishikeshd997

/*Following code should work for (n) applications/users*/
a!localVariables(
  local!users: {"aaa", "bbb", "ccc"}, /*set your users here*/
  local!applications: {}, /* insert/initialize your application CDT here */
  local!countOfUsers: count(local!users),
  a!forEach(
    items: local!applications,
    expression: {
      updatecdt(
        fv!item,
        {
          user: index(
            local!users, 
            if(
              fv!index > local!countOfUsers, 
              if(mod(fv!index, local!countOfUsers) = 0, local!countOfUsers, mod(fv!index, local!countOfUsers)), 
              fv!index
            ), 
            ""
          )
        }
      )
    }
  )
)

8 replies

richardm900458
September 29, 2021

Quick question: any reason for not using a!update()?

richardm900458
September 29, 2021


Edit: this was my approach but I recommend the one by Hrishikesh Dixit.

a!localVariables(
    local!UserList : {touser("ABC"),touser("PQR"),touser("GHJ")},
    local!LoanApplicationList: /*insert her how you get your application CDT Array*/,
    a!forEach(
      items: local!loanApplicationList,
      expression :  a!localVariables(
          local!application: fv!Item,
          local!Index: fv!Index,
          a!forEach(
            items:reverse(enumerate(count(local!userList))+1),
            expression: 
                if(
                    mod(local!Index/fv!item)=0,
                    updatedictionary(
                      dictionary: local!application,
                      fieldsAndValues: {
                        user: index(local!UserList,fv!Item,null)
                      }
                    ),,
                    {}
                )
            )
        )
    )
)

September 29, 2021

/*Following code should work for (n) applications/users*/
a!localVariables(
  local!users: {"aaa", "bbb", "ccc"}, /*set your users here*/
  local!applications: {}, /* insert/initialize your application CDT here */
  local!countOfUsers: count(local!users),
  a!forEach(
    items: local!applications,
    expression: {
      updatecdt(
        fv!item,
        {
          user: index(
            local!users, 
            if(
              fv!index > local!countOfUsers, 
              if(mod(fv!index, local!countOfUsers) = 0, local!countOfUsers, mod(fv!index, local!countOfUsers)), 
              fv!index
            ), 
            ""
          )
        }
      )
    }
  )
)

richardm900458
September 29, 2021

fv!index > local!countOfUsers,   -> fv!index < local!countOfUser ?
but besides that: your solution is waaay easier than mine. I like it!

September 29, 2021

Thanks Richard. Just added that if() check to skip mod() logic for first few indices. For e.g. consider scenario of 10 applications and 3 users -- with this check, for first 3 elements it will use fv!index i.e. {1,2,3}, whereas beyond 3 it will use index as per mod() expression that is configured.