How to update a value of a local variable inside for each loop

A Score Level 1

Hi,

How can I update the value of a local variable inside the for each loop, for eg I have an array of CDT items and whenever a condition matches inside the for each loop then I need to put that CDT item in a local variable and the value which I have used to check the condition in another variable. Below is the same code snippet, the snippet which is highlighted in red color text is what I need to achieve

a!localVariables(

local!CDTArray: {{AID: 1, GROSS_AMOUNT: 120000, NET_AMOUNT: 90000}, {AID: 2, GROSS_AMOUNT: 115000, NET_AMOUNT: 80000}, {AID: 3, GROSS_AMOUNT: 110000, NET_AMOUNT: 85000}, {AID: 4, GROSS_AMOUNT: 105000, NET_AMOUNT: 75000}}

local!unmatchedTotalAmt: {},

local!unmatchedCDTItem: {},

a!foreach(

items: local!CDTArray,

expression:{

if(fv!item.GROSS_AMOUNT <= 110000,

{

/* put the GROSS_AMOUNT value in local!unmatchedTotalAmt variable */

/* put the current CDT value in local!unmatchedCDTItem variable */

},

null

)

}

)

)

Parents
  • In Appian, you need to change you way of thinking from "update value of X variable", to "define local variable using value...".  A ForEach loop will return a value as the result of each loop, not push values to other previously defined variables.  This changes somewhat if it's used in the target of a saveInto resulting from direct user interaction on a form, but this does not seem to necessarily fit your example.

    (Also, when inserting a big code snippet like you did, it would help a lot to use the Insert --> Insert Code functionality here to preserve formatting and indentation..)

    Please find attached, where I've accomplished what you want by first capturing an array of the indices of the original array which satisfy your condition, then defining a final variable containing just those members of the original array.

    a!localVariables(
      local!CDTArray: {
        {AID: 1, GROSS_AMOUNT: 120000, NET_AMOUNT: 90000}, 
        {AID: 2, GROSS_AMOUNT: 115000, NET_AMOUNT: 80000}, 
        {AID: 3, GROSS_AMOUNT: 110000, NET_AMOUNT: 85000}, 
        {AID: 4, GROSS_AMOUNT: 105000, NET_AMOUNT: 75000}
      },
      /*local!unmatchedTotalAmt: {},*/
      /*local!unmatchedCDTItems: {},*/
      
      local!unmatchedIndices: a!foreach(
        items: local!CDTArray,
        expression:{
          if(
            tointeger(fv!item.GROSS_AMOUNT) <= 110000,
            fv!index,
            {}
          )
        }
      ),
      local!unmatchedItems: index(
        local!CDTArray,
        local!unmatchedIndices
      ),
      local!unmatchedItems
    )

  • Hi ,

    Thanks for your response, this is really well explained the way we need to think while coding in Appian. I have a query i.e., when I want to perform 2 different operations in for loop like want to check 2 different conditions and capture 2 different indices when the condition matches, so in this case I may need to execute the for each loop twice which may increase execution time. So is there any optimized approach which can be followed for this requirement. Also if I want to exit the loop in-between whenever a condition matches, then is there any way availbale in Appian or do I need to loop through all the items everytime.

    Thanks. 

  • so in this case I may need to execute the for each loop twice which may increase execution time. So is there any optimized approach which can be followed for this requirement

    The example I posted above demonstrates that in many cases, you can run the forEach loop just once and use that run for subsequent things that don't require a new loop to be run.  An extension to this, i.e. if you need to check 2 separate conditions for each member of the loop, would be to build a data dictionary to return for each member, where each item in that data dictionary would contain a flag or other metadata related to how that member of the original array does or does not satisfy the condition.

    Also if I want to exit the loop in-between whenever a condition matches, then is there any way availbale

    a!forEach() loops are not designed to be exited in the middle of execution.  You will need to design any forEach loops with the assumption that they will execute once for every member of the "items" array, without exception.

  • Hi ,

    Thanks for the response, thats well explained.

Reply Children
No Data

 Discussion posts and replies are publicly visible