Skip to main content
March 11, 2023
Question

Nested ForEach and localVariable

  • March 11, 2023
  • 16 replies
  • 1 view

So, I am trying to capture and store nested data from USDA Database - API (image below) 

Now I am trying to store this data in a record - fields being gtinUpc, number, name, amount, and unitName.  I am trying to use the forEach-nested loop example from 

https://docs.appian.com/suite/help/23.1/fnc_looping_a_foreach.html.

However, when I try to nest another forEach and iterate over the foodNutrients, I get errors.  I have tried moving lines around - for example, move line 21 to outside the inner a!localVariables - nothing seems to work. 

As always, any and all feedback/assistance would be appreciated.

Thanks

16 replies

richardm900458
March 11, 2023

Hello Mana, 

the ":" notation is just a declaration at the beginning of your form or expression rule to define your locals. 
not assignend values later:

so here its lesse with the iteration lopping its with your ":" notation
Do you want to create a new record value set or update an existing one?

man0001Author
March 11, 2023

Helo Richard:

I am trying to populate the record values for now but in the long run, these records will be updated based on any updates done by the API-source (USDA Foods).

Ma

richardm900458
March 11, 2023


perhaps this one is helping you.
So what I am saying: I see rather an issue how you create/update data IN your a!forEach than your a!foreach logic itself.

stefanhelzle0001
Brainy
March 12, 2023

In line 18, you try to declare a local variable without using a!localVariables(). That will not work. I suggest to spend a bit of time to really understand how variable scoping works, and then return to your actual implementation. This is what I always do when there is something happening I do not understand.

man0001Author
March 12, 2023

Stefan:

I agree with you about spending time trying to understand the concepts - hence my attempt at doing all these exercises.  However, I seem to be missing something fundamental about them.  

This was my attempt at trying to emulate the example from the documentation  (https://docs.appian.com/suite/help/23.1/fnc_looping_a_foreach.html).

I don't understand why the code on the left side works but the code on the right doesn't.  The code on the left has an 'undeclared local!variable' whereas the code on the right side has the local!variable declared.

stefanhelzle0001
Brainy
March 12, 2023

First, the syntax checker in Appian is not perfect. Second, Appian sometimes tries to evaluate code which violates that syntax. This might or might not create the expected outcome in certain scenarios.

I can only repeat it. Trying to declare a variable in the wrong place will get you into trouble sooner or later.

Comments on your code snippets:

LEFT: You try to create a dictionary. What is the point of trying to use local variables as the field names? https://docs.appian.com/suite/help/23.1/parts-of-an-expression.html#dictionaries

RIGHT: You try to define a local variable outside of a!localVariables. Why? By "outside" I mean "Not as the first parameters"!

You cannot declare a new local variable just somewhere in the expression part. Think of it like this. a!localVariables declares a new execution context. Rule inputs and declared local variables define the available variables in that context. Then you code is executed inside this, now static and immutable, context.

Definition: "Local variables are defined in the a!localVariables function. You can define both the name and the value of the local variable, then use those variables in the last parameter of the function"

docs.appian.com/.../Local_Variables.html