ServiceNow Flow Designer: Convert List Collector to String of Friendly (Display) Values

jmiskey
Kilo Sage

We have a situation in a Flow where a user can select multiple softwares from a request form.  We then want to show all those selections in the Description field of a Catalog Task in that Flow.  The issue is, if you drag the data pill of the List Collector Variable into the text of the Description field, it shows you the sys_ids of the selections, separated by commas, and not the display names of those softwares.  So I need to convert this list of sys_ids to a list of display names.

 

I am thinking this could be a handy thing to make a generic Custom Action, as I could see this functionality being re-used in other Flows too.  My thought is to create a Custom Action that iterates through the selections in a Glide Record, looks up the display value, and writes a comma-separated string of the display value.  I have not done that yet, but I think that I should be able to do all of that. 

 

My question is this: is there some way in my script for this Custom Action to identify the Reference table the List Collector is coming from?  If not, I will need to include the table name as an Input for my Custom Action.  But I was thinking, there should be a way to figure it out from the List Collector variable itself.  I am just not sure how.

 

And I am not quite sure if I need to specify the field name I want to return, or if I can just use GetDisplayValue.

 

Basically, I am just trying to figure out if there is a way that instead of having these three Inputs for this Custom action:

1. List Collector Variable value

2. Reference Table

3. Field to Return

to just have:

1. List Collector Variable value

and derive the second two from the first.

Or am I asking too much?

1 ACCEPTED SOLUTION

I got it!  What you said gave me a clue, when you said that the Input Value is a Glide Record.  I should just be able to iterate through that and grab the display values.

 

So, I just have this one input now (which is my List Collector):

jmiskey_0-1742397989893.pngjmiskey_1-1742398023271.png

And this script:

(function execute(inputs, outputs) {

    var str = '';

    //loop through list collector (Glide Record)
    var gr2 = inputs.lc_variable;
    while(gr2.next()){
        //get Display Value of record
        str += gr2.getDisplayValue() + ',';
    }

    //remove last comma from string
    str = str.substring(0, str.length-1);

    //set string to output
    outputs.output_string = str;

})(inputs, outputs);

and it returns exactly what I want!

 

 

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

you can use flow variable and set the display value and then use it, No custom action required

var arr = [];
var sysIds = fd_data._1__get_catalog_variables.groups_to_be_removed.toString(); // give the list collector variable name here
var rec = new GlideRecord('sys_user_group'); // table to be queried here
rec.addQuery('sys_id', 'IN', sysIds);
rec.query();
while(rec.next()){
arr.push(rec.getDisplayValue());
}
return arr.toString();

list collector display value using flow variable.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

That is not quite what I am looking for.  I think you are missing my point.  I would like to create a Generic Custom Action, where you just feed in the value of the List Collector selections, and it spits out the Display Values of those selections in a comma separated string.

 

The issue with the approach you proposed is that every time you wanted to do something like this in a flow, you would need to add in that code.  I am trying to create a No Code reusable Custom Action, so if we want to re-use this logic in other Flows, we do not need to add custom code each time.  We just need to feed in the List Collector Variable values, and it spits out the list of friendly display values.

 

So I am not looking for a "one-time" fix.  I am looking for a re-usable Custom Action that we can use in other Flows (I have many team members that aren't that strong in coding, so I want to make it as easy for them as possible).

@jmiskey 

then create a custom action and pass these as inputs. These are sufficient enough.

1. List Collector Variable value

2. Reference Table

3. Field to Return

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Once again, I do not think you are understanding what I am asking for.  I can already do that.  I want to see if there is a way to go it with just the List Collector Variable being passed to the Custom Action (one variable instead of three).  My thought is there may be a way to determine the table that the values are coming from without having the user to explicitly enter it.  

 

In any event, for some reason, I cannot even get it to work using all 3 parameters.  I was able to make it work in a Background Script like this, where I hard-code in the values that are being selected, i.e.

	var lc_variable = '9a5683ae0feca240e3b522d8b1050ee2,c5012dbadb3eec14265141db139619be';
	var ref_table = 'cmdb_software_product_model';
	var dis_field = 'display_name';

var str = '';
    
    //loop through variables of list collector
    var softwares = lc_variable.split(',');
    for(var i = 0; i < softwares.length ; i++){
        //build glide record to look up record
        var gr = new GlideRecord(ref_table);
        gr.addQuery('sys_id',softwares[i]);
        gr.query();
	  
	  while(gr.next()){
            //add display value to string being built
		//str += gr.getDisplayValue() + ',';
            str += gr.getValue(dis_field) + ',';
	  }
    }

    //remove last comma from string
    str = str.substring(0, str.length-1);

    //set string to output
    gs.print('Output: ' + str);

and this perfectly returns the values I expect.

 

However, if I try to incorporate this logic into a Custom Action, it does not return anything.  Here is how I have that Custom Action set-up:

jmiskey_0-1742392493946.pngjmiskey_1-1742392533138.png

Here is the script:

(function execute(inputs, outputs) {

    var str = '';
    
    //loop through variables of list collector
    var softwares = inputs.lc_variable.split(',');
    for(var i = 0; i < softwares.length ; i++){
        //build glide record to look up record
        var gr = new GlideRecord(inputs.ref_table);
        gr.addQuery('sys_id',softwares[i]);
        gr.query();
        //add display value to string being built
        while(gr.next()){
            str += gr.getValue(inputs.dis_field) + ',';
        }
    }

    //remove last comma from string
    str = str.substring(0, str.length-1);

    //set string to output
    outputs.output_string = str;

})(inputs, outputs);

jmiskey_2-1742392600857.pngjmiskey_3-1742392633121.png

 

And here is how I am calling it in my main Flow:

jmiskey_4-1742392678462.png

 

I tested trying to send a hard-coded value to the output, and confirmed that worked.  So I know the mapping to the output value is working.  I tried hrd-coding various variables in my script, and counting the records it returns, and it always returned 0.

 

So I cannot figure out why my code works in a background script, but not in my Custom Action script.  Can anyone see the issue?