Nested ForEach and localVariable

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

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    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.

  • 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.

  • 0
    Certified Lead Developer
    in reply to MaNa

    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

Reply
  • 0
    Certified Lead Developer
    in reply to MaNa

    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

Children