- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2025 01:23 PM
I am trying to create a Custom Action in Flow Designer that will take the input of a List Collector, and output an Array of values from another field from the same table.
My Table has the following fields:
- group_name (display field)
- ad_group
So the select the values they want from the List Collector and my Custom Action is supposed to return an Array of the appropriate ad_group values from each selection.
Here are the Inputs in my Custom Action:
And then I have a Script step that has the following Input Variables:
And here is the Script:
(function execute(inputs, outputs) {
var gr1 = inputs.list_col;
var fld = inputs.fld_nm;
var ads = '';
//loop through records in list collector
gr1.query();
while(gr1.next()){
ads += gr1.getValue(fld) + ',';
}
//remove last comma from string
if(ads.slice(-1) === ','){
ads = ads.slice(0, -1);
}
//convert string to array
outputs.ad_array = ads.split(',');
})(inputs, outputs);
and here are the Output variables for the Script section:
Lastly, here are the Outputs of the Custom Action:
So, the weird thing is when I try to use this in my Flow, if I choose more than 1 selection in my List Collector, it works perfectly. But for some reason, if I only choose a single selection, it "doubles up" the value that it outputs.
Here is a log of one of these situations, where you can see a single value in the List Collector, but two values in my outputting array:
Does anyone have an idea why this is happening when I make a single entry into the List Collector, or have any idea on how to fix it?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 11:44 AM
After trying many of the recommendations and finding no success, I went back to my original code. I added a loop counter for the "while" loop and discovered that when I made a single selection, it was looping through twice. No idea why it is doing that, but that gave me an idea on how to fix it. I simply compared the value of the current iteration to the value of the previous one, and only included it if it was different. That seemed to finally work!
Here is the final code I ended up using. Thanks to all those who tried to help.
(function execute(inputs, outputs) {
var gr1 = inputs.list_col;
var fld = inputs.fld_nm;
var ads = '';
var curValue = '';
var prevValue = '';
//loop through records in list collector
gr1.query();
while(gr1.next()){
//get desired field value of current record
curValue = gr1.getValue(fld);
//check to see if value differs from previous value
if(curValue!=prevValue){
//if it is, add to running list
ads += curValue + ',';
}
//reset previous value
prevValue = curValue;
}
//remove last comma from string
if(ads.slice(-1) === ','){
ads = ads.slice(0, -1);
}
//convert string to array
outputs.ad_array = ads.split(',');
})(inputs, outputs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2025 01:55 PM
Helo @jmiskey
The list collector is a string with comma separated values. So, that's not how you read it.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 05:24 AM
This is the method I initially tried, and it didn't work. So I tried it again, using your method, but it still does not work. It does not return anything.
So I am curious, did you actually try to use this in a Custom Action Script where you are feeding in the List Collector as a variable? I am thinking that you probably did not, since I don't think you would use "gs.print" in the Script of a Custom Action.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 05:27 AM
check my response below shared 8hours ago
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2025 01:04 AM - edited 04-05-2025 02:21 AM
Hi @jmiskey
You need to modify your script. Try the below script, it'll work.
(function execute(inputs, outputs) {
var list_col_sys_ids = inputs.list_col;
var fld = inputs.fld_nm;
var ads = [];
var rec = new GlideRecord("<table_name>"); // Replace with your table name
rec.addEncodedQuery("sys_idIN" + list_col_sys_ids);
//loop through records in list collector
rec.query();
while (rec.next()) {
ads.push(rec.getValue(fld));
}
outputs.ad_array = ads;
})(inputs, outputs);
Hope this helps.
Regards,
Siva