article by Frank Nimphius, September 2019
When working with real data, objects typically contain more content (attributes) than being displayed on a map or in a list within a bot conversation. Skills in Oracle Digital Assistant obtain access to live data to display through custom components saving the data as an object, or an array of objects into a dialog flow variable. For example, the menu of pastas shown in the image below does not contain information about carbonate, calories, protein and salt.
However, when selecting a pasta product, the information should be displayed as shown in the image below:
So, based on the user selection in the card layout (which could be a list-of-values too), you want to access the object within the data array to obtain detail information about the pasta. Below image shows the mockup data array used in the sample. In a real implementation, the variable value (array) would be set through a custom component.
The menu shown in the first image was built using the System.CommonResponse component. The image below shows the card-layout menu configuration.
Notice how the selectedIndex variable (a variable of type "int" defined in the dialog flow) is assigned a value through an Apache FreeMarker expression ${pastas?index}. The pastas dialog flow variable holds the array of pasta objects and is referenced from the System.CommonResponse component's iteratorVariable property. The array referenced by the iteratorVariable property is iterated over by the UI component to create the card layouts. In doing so, the iterator tracks the index position the current stamped object has in the array.
Note: Alternative solutions would have been to save all attributes of a selected object in their own context variables, or to save the selected object as a whole in a variable. The first option, to use dialog flow variables for each attribute is hard to manage and also doesn't work well for objects with a long list of attributes. Hope you agree. The second option creates redundant copies of an object. So keeping track of the index of an object on the displayed data arrays appears to be the simplest option to use.
So when displaying the selected object's content, for which again a System.CommonResponse component is used, the array index is used within a Apache FreeMarker expression to access the object. The image below shows how the selected index is used to access an object in the array to display attribute values.
So notice the expression ${pastas.value[selectedIndex.value?number].<attributeName>}. In the sample, pastas is the dialog flow variable that holds the data array. The selectedIndex variable holds the array index of the pasta selected in the card layout. The trick that makes this simple solution possible is to use ?number to turn the index from an int to a number format understood by Apache FreeMarker expression.
Confused about what "|-" does and where it comes from? Have a look at this article for explanation.
Related Content
TechExchange Quick-Tip: Accessing Attribute Names and Values of a Data Object
TechExchange Quick-Tip: How-to Test Apache FreeMarker Expressions in Oracle Digital Assistant
TechExchange – Using Apache Freemarker Expression in Oracle Intelligent Bots
Download the Sample Skill
Access Product Documentation and Tutorials