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
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 ":" notationDo you want to create a new record value set or update an existing one?
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
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.
Hi Richard:
You are right in that is what I am trying to do. However, I have nested data to process and that is where I am getting errors.
The data I want is List of variant with two a "product code" and detailed information below that. I would like to store the data in a table similar to the image at the bottom.
I know this must be basic - but I am trying to learn Appian.
super stupid question? did you watch the record videos in the appian academy?get away from the idea of nesting (concept of the old "CDT" world)try to use record relations between records. so your task would be getting a correct foreign key relation between your two records. Idea:"product code"one to many relationfood nutrients (or how ever the element should be called)why not using a record here with the data structure you like to have?
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.
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!
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.