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
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
Discussion posts and replies are publicly visible
Quick question: any reason for not using a!update()?
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) } ),, {} ) ) ) ) )
/*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 ), "" ) } ) } ) )
fv!index > local!countOfUsers, -> fv!index < local!countOfUser ?but besides that: your solution is waaay easier than mine. I like it!
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.
ah i see.and agree. Like I said. I like it. but do you need this if clause? I think mod() should work for fv!index < local!countOfUser as well. Any particular reason why you treat the first iteration of the usrers separatly?
Yep, that will work without if clause however just thought to skip for the 1st iteration since we can use directly use fv!index (this will reduce number of mod() checks, shouldn't really impact the overall expression performance though)
Thank you Hrishikesh and Richard for your quick replies, really appreciate it. Solution provided by Hrishikesh worked like a charm. Thanks guys.