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

  • Stefan,

    Thanks for your patience in helping me understand these concepts.

    As for the code on the RIGHT and the 'definition' of local variable, are you referring to line 42 (local!currGTINUpc)?  That variable is declared on line 30 - and is being given a value on line 42 - at least that is what I am trying to do.

    At the end of the day, I am attempting to take information from a web integration result (which is the dictionary) and put it into a recordType.  The web integration is a list of dictionary with a sub-list of dictionary.  I am trying to traverse through the main dictionary and for each main dictionary traverse the sub-dictionary. 

    If there is a better approach, please do let me know.

    Thank you!

  • 0
    Certified Lead Developer
    in reply to MaNa

    Appian expressions use a functional paradigm with immutable variables. You cannot change the value of a variable after initial definition.

    Your overall approach is good. You seem to still fight with the Appian programming paradigm.

  • 0
    Certified Senior Developer
    in reply to MaNa

    Hey MaNa, do you have an Appian Mentor near by. I think a face to face session with a more experienced designer might help to resolve some misunderstandings :) and i can always recommend to do your first steps with the appian academy.

  • That variable is declared on line 30 - and is being given a value on line 42

    To reiterate and simplify upon the prior correct responses by Stefan and Richard: this is not how local variables work.  You need to set the value of the local variable where you declare it.  Any use of a local variable needs to be done inside the same a!localVariables() statement in which it was declared.  The code snippets you've posted here all violate both of these tenets.

  • Mike, Stefan, Nasirs, and others:  

    Thank you for all the responses & feedback - very much appreciate it!

    I think I have a better understanding of a!localVariables.  Going work on how to connect recordType to web integration using the Space X tutorial.

    Ma

Reply Children
No Data