Skip to main content
Known Participant
Mar 1, 2023 7:34 AM UTC
Question

Update value of a local variable to new values inside a foreach loop

a!localVariables(
local!principalAmount:ri!loanAmount,
local!totalMonths: ri!tenure * 12,
local!interestForOneMonth: round(ri!interest / 12, 2),
local!interest: (
local!interestForOneMonth * local!remainingAmount,
) / 100,
local!remainingAmount:local!principalAmount-local!interest,

a!forEach(
items: enumerate(local!totalMonths) + 1,
expression: {

local!interest,
local!principalAmount,
local!remainingAmount,

a!save(local!remainingAmount,local!principalAmount-local!interest,),
a!save(local!principalAmount, local!remainingAmount),

}
),
)

For each iteration of the for loop i want to update the value of local!principalAmount and local!remainingAmount as shown above. Kindly let me know how we do this in Appian.

    16 replies

    harshitb6843
    Brainy
    Mar 1, 2023 7:39 AM UTC

    You cannot change the values of a variable in a rule once assigned. Also, a!save() only works on human interactions/events and hence it won't work. 
    You can just create a new dictionary, or array and just assign it to a new variable and consume it. Or directly consume it without assigning it. 

    Known Participant
    Mar 1, 2023 7:43 AM UTC

    I want to 

    remaininamount=principalamount-interest,

    principalamount=remainingamount

    for each iteration. Could u pls let me know how to do this?

    harshitb6843
    Brainy
    Mar 1, 2023 7:45 AM UTC

    What is your end goal? Tell me your expected output. 

    stefanhelzle0001
    Brainy
    Mar 1, 2023 7:47 AM UTC

    Check my blog post about how to implement more complex algorithms in Appian.

    appian.rocks/.../

    Known Participant
    Mar 1, 2023 7:54 AM UTC

    Hi Stefan, saw the post. Could u help me with applying recursion in this scenario that i mentioned above?