Ankur Bawiskar
Tera Patron

Many a times there is a need to use list collector variable in the Flow Designer for multiple purposes such as creating 'n' number of catalog tasks or approval records based on list collector variable value count.

So we will have to use 'For Each' Action in Flow designer.

I have seen numerous questions around this so thought of creating a blog with the steps so that it helps community members.

Limitations: There is a known issue that it cannot be done directly KB0793340

Reason -> The Get Catalog Variables action returns a list collector as a comma separated string rather than an array. So we cannot use the 'For Each' action directly in your flow.

Resolution: So here is a workaround

  1. Create a custom flow action that takes list collector value string as the input
  2. Parse the string into an array
  3. Then output the array to be used in the For Each Loop.
  4. Then call this custom action in your flow and then use the output as per your needs

Steps:

1) Create custom flow action

a) Name - List Collector Convert to Array

2) Define the input variable as String

find_real_file.png

3) Create Script step

a) Map the input variable to the variable being used in script step -> by selecting the data pills -> inputs variable

b) Create output variable of type -> Array.String

Edit: As per few members update this is not working post upgrade from Orlando to new instance

So please change the Output type as Array.Object instead of Array.String

c) Use this script: To convert String to Array of Object (Edited)

(function execute(inputs, outputs) {
// ... code ...
  
  outputs.variable = inputs.variable.toString().split(',');
  
})(inputs, outputs);

find_real_file.png

find_real_file.png

4) Define the Output

find_real_file.png

5) Publish the Action

find_real_file.png

Usecase:

  1. Consider you have 1 list collector variable on your catalog item referring to Users table (sys_user)
  2. You want to set approvers based on how many users are present in list collector. If 3 users are selected then 3 approvers or 3 catalog tasks are to be created

Steps to implement with example: Trigger - Service Catalog

I assume you must have attached the flow to your catalog item.

  1. Use the "Get Catalog Variables" Action
  2. Call the custom Action "List Collector Convert to Array" and pass the list collector values as input
  3. Do a For-Each on the result from the custom action (array of sys_ids)
    3.1 Do a GlideRecord Lookup for the User based on the for-each value (sys_id)
    3.2 Use the "Ask For Approval" Action as you want and set the User as Output from 3.1 step

Screenshots:

find_real_file.png

find_real_file.png

find_real_file.png

 

 

find_real_file.png

find_real_file.png

 

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful & also Bookmark.
Thanks,
Ankur Bawiskar

ServiceNow MVP 2020,2019,2018

My Articles & Blogs

103 Comments
Flemming Skytte
Tera Contributor

Thanks! This was just what I needed to complete my Catalog Item using List collector and and Flow.

Ankur Bawiskar
Tera Patron

@Flemming Skytte Olesen 

Glad to help.

Please remember to mark blog as helpful and also bookmark it.

Madala Chaitany
Giga Guru

Hi Ankur,

I need to send two group approval at same time.

by using your code it's going for one by one approval.

 

Thanks in advance!

Chaitanya

sakshijain09
Tera Contributor

Hi Yamuna,

I am also trying with the same method but its not working..Please guide

Poorva Bhawsar
Mega Sage

Not able to add that variable under for each loop.

find_real_file.png

Praseena
Tera Contributor

Hi @Ankur Bawiskar  ,

Thank you for the solution. This solution sends approval one by one but my requirement is to send approvals to all users at once.  

Can you please help me with the same?

 

Thanks,

Praseena

Asma Nadaf
Kilo Contributor

It was very good Sir

Vitor Taddei1
Tera Contributor

To all who was receiving the [object GlideObject], i could get the correct value when i set a variable before passing it to the action, so before passing the List to the action, transform it to String and then pass to the action.

find_real_file.png

Amit Naik1
Tera Contributor

Hi @Ankur Bawiskar ,

Do you have any workaround for this as I am also facing the same issue?

Thanks,

Amit

Kerry Hormann
Tera Contributor

Thanks for providing this write-up.  But what an utterly horrible problem to have to fix!  I tried for quite some time to find a different way around it, but this is just a broken feature.

ServiceNow NEEDS to fix List Collector iteration in Flow!

Even when we employ this hack, it presents another issue with resolving the values (sys_id) to Glide records - the LC's table name is not available in the data stream (pills)!  So, yes, you can look up records, but you have to KNOW the table and hard-code it.  Boooooo.