The CreatorCon Call for Content is officially open! Get started here.

Push to array output variable in subflow

Bradley Koerner
Tera Contributor

In Flow designer, I have an output variable that is an array of objects. Within my subflow, I am iterating through a list of records and performing some operations (assembling data, calling a REST API, etc.) There are a handful of results for each given record, so I want to capture all of those results within my array.

My problem is that, while there is a Flow Logic to "Assign Subflow Outputs", it seems to only allow me to set my output array and/or its individual elements. For each of my records, I want to be able to push to the array. Is that possible?

Basically, I want Flow to be doing something like this: 

while (gr.next()) {
  var result = doThings();
  output.my_array.push(result);
}

But it only appears to let me do this:

while (gr.next()) {
  var result = doSomething();
  output.my_array = result
  // or
  output.my_array[0] = result;
}

It seems to me, only allowing setting of the array or hardcoded indexes completely defeats the purpose of an array.

13 REPLIES 13

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Bradley,

Instead of pushing each result into an output, try pushing all results into an array first and then setting the array to output.

Example.

while (gr.next()) {
  var myArray = [];
  for (var i=0; i<.....; i++) {
    var result = doThings();
    myArray.push(result);
  }
  output.my_array = myArray;
}

1derbman1
Tera Contributor

Hi Bradley, 

There appears to be an action in Flow designer called JSON Processing that will take any raw json string and convert it to an Array.Object which can then be used in later For loops etc which should solve your problem. Here is a couple images to help. I have not used this yet, but have have used a similar in action step called JSON Parser that will do the same and allow an action to include an Array.Object as an output. Ultimately if that Action does not do what you want you can create an action which has one step (JSON Parser) and then output the Array.Object from there to be used as desired.

I believe that option is only available if you have Integration Hub Professional or Enterprise.

 

https://www.servicenow.com/content/dam/servicenow-assets/public/en-us/doc-type/legal/snc-addendum-in...

1derbman1
Tera Contributor

If you do not have Integration Hub you can still do this same thing by creating a custom action > adding a script step and in the scripting step taking the rawJSON input putting it in JSON.parse(<rawJSON>); and setting that result to a custom output that fits the JSON structure. That JSON output pill can be an Array.Object which is then available for later actions to consume.