How do I create and return a useable array of strings from an action in Flow Designer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
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:
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.
