- 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-07-2025 06:20 AM
Nope, that still returns nothing at all. Are people actually testing the solutions before posting them? I get the feeling that people are tossing out untested ideas, or at least not using the same requirements that I have (which is why I went through all the detail in my original post to show how each field and input and output in my Custom Action is set-up).
If I need to change the type of any of the input or output variables, please let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 07:00 AM
We can't replicate the complete setup as it does take time and we may not have the exact same table structure etc in our instance.
But what I replied is based on what you shared.
You didn't share what debugging did you perform by adding gs.info() in the script I shared.
try this and share the logs
(function execute(inputs, outputs) {
var listCollectorValues = inputs.list_col.toString();
var fld = inputs.fld_nm.toString();
var ads = [];
gs.info('list collector value' + listCollectorValues);
gs.info('field to fetch is' + fld);
//loop through records in list collector
var rec = new GlideRecord('tableName'); // give here the table being referred by that list collector
rec.addQuery('sys_id', "IN", listCollectorValues);
rec.query();
gs.info('row count is' + rec.getRowCount());
while (rec.next()) {
var val = rec[fld];
ads.push(val.toString());
}
var arrayUtil = global.ArrayUtil();
ads = arrayUtil.unique(ads);
//convert string to array
outputs.ad_array = ads;
})(inputs, outputs);
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-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-07-2025 08:29 PM
I am still curious to know how are you able to iterate on that inputs.list_col as it's a string type
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-08-2025 04:04 AM
Yeah, I am not exactly sure how or why it treats it like a GlideRecord, but it does seem to, even without telling it what table those sys_ids belong to.