Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How do I create and return a useable array of strings from an action in Flow Designer?

jeremyduffy
Kilo Guru

I'm working with a few different record types that have different "state" values that are all numbers instead of strings. As I loop through each returned record, I could simply look up sys_choice to get the string representation of the number, but that seems computationally heavy. In any normal instance, I'd simply dump the sys_choice values to an array and then reference them later (less DB hits).

 

I'm trying to approximate that behavior using an action. I've built one that simply takes a table name as input and returns a list of choices in number->text_value format like so:

 

(function execute(inputs, outputs) {

    function nonEmptyString(v) {
        return (v == null) ? '' : String(v).trim();
    }

    var tableName = nonEmptyString(inputs.table_name);

    // Object map: { "1": "New", "2": "In Progress", ... }
    var stateMap = {};

    if (tableName) {
        var gr = new GlideRecord('sys_choice');

        gr.addQuery('name', 'STARTSWITH', tableName);
        gr.addQuery('element', 'STARTSWITH', 'state');

        gr.query();

        outputs.test = '';
        while (gr.next()) {
            var val = gr.getValue('value');  // e.g. "2"
            var lab = gr.getValue('label');  // e.g. "In Progress"
 
            // IMPORTANT: allow "0" and negatives; only exclude null/empty
            if (val !== null && val !== '') {
                stateMap[String(val)] = lab;
                outputs.test += val+lab+ " - ";
            }

        }
    }

    outputs.states = stateMap;

})(inputs, outputs);

 

I originally tried returning an object type, but it was always empty ( {} ). I changed it to array.string and finally got something that looks like this: 

 

 

This looks usable, though when I look at the actual flow, I can't figure out how to access the array. In theory, the number value is a string index and it should return the text output, but how? 

 

I tried setting a variable to the value using javascript like this:

return fd_data._2__record_states_to_array[fd_data._3__for_each.item.state]

But it doesn't work. It gives me some kind of snapshot error actually. 

Looking at the pills directly, I see this:

Screenshot 2026-02-20 102104.png

Which gives me no clue how to access the text value I need using the string "state" code directly. How do I tie this all together?

 

I could rebuild the action to take the code and table as input and output the text name of that state, but that would mean calling the action once for every record processed which could be thousands of times.

2 ACCEPTED SOLUTIONS

Brad Bowman
Mega Patron

I'll relate this to something we're doing to see if it applies to this specific use case.  I've used Output Variables with the type Array.Object and Array.String interchangeably.  The Script step must create an array of values - it looks like your array may be one concatenated string, not an actual array of strings (your screenshot on this is not visible for some reason). So my script builds a string separated by commas, then the last line is like

outputs.variable = myString.split(',');

to assign the script output variable the value of a populated array.  My Flow calls this action, immediately followed by a 'For Each' flow logic activity where the Items value is populated with the Array data pill ('states' in your case).  Within this loop I can now access each actual array value in a lookup or whatever activity using the states_child0 pill.

 

 

View solution in original post

yashkamde
Kilo Sage

Hello @jeremyduffy ,

 

Firstly, Flow Designer doesn’t support arbitrary JS objects as outputs.

AND "fd_data._2__record_states_to_array[...]" fails because FD object don’t allow dynamic indexing into arrays by string keys.

So replace stateMap as [] - (array)

//Replace 
if (val != null && val != '') {
                states.push({
                    code: String(val),
                    label: lab
                });
            }

outputs.states = states;  // Array of {code, label}

 

Your action now returns states as an array of objects.
In the Flow, you can use a For Each loop on states and compare item.code to your record’s state.

If my response helped mark as helpful and accept the solution.

View solution in original post

5 REPLIES 5

Brad Bowman
Mega Patron

I'll relate this to something we're doing to see if it applies to this specific use case.  I've used Output Variables with the type Array.Object and Array.String interchangeably.  The Script step must create an array of values - it looks like your array may be one concatenated string, not an actual array of strings (your screenshot on this is not visible for some reason). So my script builds a string separated by commas, then the last line is like

outputs.variable = myString.split(',');

to assign the script output variable the value of a populated array.  My Flow calls this action, immediately followed by a 'For Each' flow logic activity where the Items value is populated with the Array data pill ('states' in your case).  Within this loop I can now access each actual array value in a lookup or whatever activity using the states_child0 pill.

 

 

Both answers said the same thing, but yours was first so I'm accepting it. What I understand is that the issue is that I can't refer to an array directly in a flow - I have to run it through a for-each at best. That's disappointing, but that's what it is.

Thanks for confirming!
Just a quick note - you can actually accept multiple answers if more than one is helpful. 😊

 

That way, future readers can see different perspectives or explanations that might add value.

I did not know that. Thank you!