How to update the array inside the for each loop and to use the updated value in further iteration?

Hi,

I have a requirement to update the actual array value and the updated values in the array I need to use it in my further iterations in a for loop, for eg., below is the sample code for my requirement

a!localVariables(
local!arrayToLoop: {
{credit: 10, debit: 100, txn1: 0, txn2: 150, txn3: 0},
{credit: 100, debit: 0, txn1: 0, txn2: 150, txn3: 0},
{credit: 0, debit: 100, txn1: 0, txn2: 150, txn3: 0},
{credit: 0, debit: 100, txn1: 0, txn2: 150, txn3: 0},
{credit: 0, debit: 0, txn1: 0, txn2: 150, txn3: 0},
{credit: 75, debit: 80, txn1: 0, txn2: 150, txn3: 0},
{credit: 65, debit: 0, txn1: 0, txn2: 150, txn3: 0},
{credit: 0, debit: 100, txn1: 0, txn2: 150, txn3: 0},
{credit: 50, debit: 70, txn1: 0, txn2: 150, txn3: 0},
{credit: 40, debit: 100, txn1: 0, txn2: 150, txn3: 0}
},
a!foreach(
items: local!arrayToLoop,
expression: if(
fv!isFirst,
fv!item,
if(
tointeger(
fv!item.credit
) > 0,
fv!item,
updatecdt(
fv!item,
{
credit: index(
local!arrayToLoop.credit,
fv!index - 1,
0
)
}
)
)
)
)
)

Output of the above code is 

[credit:10,debit:100,txn1:0,txn2:150,txn3:0]; [credit:100,debit:0,txn1:0,txn2:150,txn3:0]; [credit:100,debit:100,txn1:0,txn2:150,txn3:0]; [credit:0,debit:100,txn1:0,txn2:150,txn3:0]; [credit:0,debit:0,txn1:0,txn2:150,txn3:0]; [credit:75,debit:80,txn1:0,txn2:150,txn3:0]; [credit:65,debit:0,txn1:0,txn2:150,txn3:0]; [credit:65,debit:100,txn1:0,txn2:150,txn3:0]; [credit:50,debit:70,txn1:0,txn2:150,txn3:0]; [credit:40,debit:100,txn1:0,txn2:150,txn3:0]

Here in my current code the actual array is not getting updated, only the fv!item is getting updated so when I check the the "credit" value of the previous ideration it is still "0" as per the original array

But the expected outout is 

[credit:10,debit:100,txn1:0,txn2:150,txn3:0]; [credit:100,debit:0,txn1:0,txn2:150,txn3:0]; [credit:100,debit:100,txn1:0,txn2:150,txn3:0]; [credit:100,debit:100,txn1:0,txn2:150,txn3:0]; [credit:100,debit:0,txn1:0,txn2:150,txn3:0]; [credit:75,debit:80,txn1:0,txn2:150,txn3:0]; [credit:65,debit:0,txn1:0,txn2:150,txn3:0]; [credit:65,debit:100,txn1:0,txn2:150,txn3:0]; [credit:50,debit:70,txn1:0,txn2:150,txn3:0]; [credit:40,debit:100,txn1:0,txn2:150,txn3:0]

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Like i said in my comment on your previous thread on this topic, there isn't such a thing as "updating a value" inside a loop of a!forEach.  That's simply not how Appian's expression rule logic works.  You will need to reset your expectations centered on how the logic does work, then develop a solution based on that. 

    For your current use case I suspect, based on what you seem to want the output logic to be, that your updateCdt() call will need to be updated to look back through multiple members of the original array, instead of just the last index.  This could be accomplished via doing an inner loop to step back 1 item, then if the "credit" value is still 0, step back 2 items, etc.

  • Hi ,

    Thanks for your response, in my actual array I have 14 fields in each array itne and for each field I need to check for value > 0 in the previous array items so in this case am going to perform an inner looping checking for previous items for all the 14 columns. Also in my array am expecting upto 10000 array items, so lets say if I have vlaue > 0 only in my 2nd array item and rest of my array item fields are '0' then every time i need to loop to step back to reach the second array item for Non-Zero value and ideally ill end up having an inner iteration upto 9999 times while am in 10000th array index of my main for loop. Please suggest if we have any other way we can handle my requirement by avoiding much of inner looping.