- 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 07:26 AM
i apologize if it did not meet your expectations. however, i needed to make sure that your steps are correctly implemented. To further provide you with answers, i have created this custom code that gives you the ability to easily pass any structure in the Glide Record object. hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2025 08:51 PM
Hello @jmiskey
Please confirm if you checked my answer. Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for my efforts and also it can move from unsolved bucket to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2025 09:23 PM
try this
1) I used array declaration to push values
2) then used ArrayUtil unique() method to remove duplicates
(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.push(gr1.getValue(fld));
}
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 05:57 AM
No, that one does not give me any output either. I wonder if you have to play around with the Input/Output variable types in order to get it to work properly.
It is very odd, the only one of the 4 methods I have tried so far which provides me any output is my original code. It just doubles up the value if I make exactly one selection. I am not sure if Scripts in Custom Actions have certain limitations. Does anyone know a way to use the method I have used so far, and to just weed out the duplicates?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2025 06:10 AM
Yes it's something to do with the type
1) the script step accepts input as string which is comma separated so update your script as this
(function execute(inputs, outputs) {
var listCollectorValues = inputs.list_col.toString();
var fld = inputs.fld_nm;
var ads = [];
//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();
while (rec.next()) {
ads.push(rec.getValue[fld]);
}
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