Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How can I transform a list of items (or a collection) in a single bundle to an array that can be iterated through?

Avatar

Level 9
Level 9

I have a working solution that I am looking to simplify or make more efficient.

There are times that a module will return a single record (Project) either through a Search Record module or a Custom API call module. In this example, I'm using both: a Custom API call so I can include parameterValues in the fields list; followed by a Search Record (for the same project) where I map the parameterValues collection that was returned in the response of the API call to the Outputs field of the Search Record module. This creates a dynamic way to pull in all of a project's custom data in the Search Record module without having to hard code the output selections.

In this example, the record that is returned is a single bundle, and that bundle is a list of items (name, ID, DE:this, DE:that). What I need to do is iterate through that list of items, not knowing in advance which DE:fields may or may not be in the list. (I'm using contains on the name to determine if actions should or shouldn't happen, and the value to determine what actions should happen).

I can get a simple array of the DE:fields by using {{keys(**.body.data.parameterValues)}} as an input to a Set Variable module - but this only returns an array of the item names, without their corresponding values.

I then set an Iterator to work through that Set Variable module with an input of {{if(contains(**.value; "DE:QTY"); **.value; "")}} (In this example, if the name of the custom field being evaluated begins with QTY, get the value of the custom field.)

I've managed to make this work, but it feels like I should be able to map both the names AND the values to an array of collections of key:value pairs.

Is there some combination of array functions (get, map, keys?) that I can use in a Set Variable module (or as in input to an iterator module) that will yield an array of all the names and values from a single bundle of items, or is there a way to break that single bundle of items into multiple bundles of key:value pairs?

So from this:

parameterValues (collection)

DE:customField1 : custom value 1

DE:customField2 : custom value 2

DE:customField3 : custom value 3

to this:

parameterValues [array]

1 (collection)

name: DE:customField 1

value: custom value 1

2 (collection)

name: DE:customField 2

value: custom value 2

3 (collection)

name: DE:customField 3

value: custom value 3

TLDR: I want to iterate through items in a single bundle (using both key and value).

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 4

I suppose we may want to consider a "convert collection to Array" function at some point.

In the meantime, this scenario blueprint will do the trick. (The tip is to iterate with keys function as you have done..then get the value of the key in each iteration using get). See attached blueprint -- which does not include my real age 🙂

View solution in original post

4 Replies

Avatar

Level 9
Level 9

That is a handy trick, Doug; but not exactly what I'm after here. I was looking to convert the collection of parameterValues in an http response to an array so it could be processed more efficiently.

Avatar

Correct answer by
Level 4

I suppose we may want to consider a "convert collection to Array" function at some point.

In the meantime, this scenario blueprint will do the trick. (The tip is to iterate with keys function as you have done..then get the value of the key in each iteration using get). See attached blueprint -- which does not include my real age 🙂

Avatar

Level 9
Level 9

Thanks Darin. I'm definitely interested in a convert Collection to Array function. This can simplify scenarios that need to perform logic that is conditionally driven by the name of list items; and subsequently their value.

Glad to know my workaround is as good as it gets and I wasn't overlooking the obvious answer, like I might have done when I was 21...