[SalesForce] Flow + @InvocableMethod: How to assign output to collection/sobject collection variable

There are methods like:

@InvocableMethod
public static List<Id> loadContact(List<String> ids) {

or

@InvocableMethod
public static List<Contact> loadContact(List<String> ids) {

I'd like to assign output to collection variable but only regular variable is available. If I try to created collection variable, validation prevents collection variable assignment:

enter image description here

The requirement is the following:

  • 1st screen – dynamic multi choice field to collect contacts/contact ids
  • 2nd screen – to display radio dynamic field with contacts selected on the 1st screen

So I thought to use apex to pass string like 'id;id;id' and return List< Id > to use that list as filter on the second screen

Thanks in advance.

Best Answer

I had the same issue and found the answer on the developer forum, posted by user Chad Barbour.

Basically your @invocableMethod must return a list of lists,

 public static List<List<PriceBookEntry>> getPBE(){
      List<PriceBookEntry> PBEs = [Select Id, Product2Id from pricebookentry where Product2.Family = 'test'];
      List<List<PriceBookEntry>> listOfPBELists = new List<List<PriceBookEntry>>();
      listOfPBELists.add(PBEs);
      return listOfPBELists;
 }

With this, in visual flow, you can assign the output to an Sobject collection of the same type and use it like you would any other sobject collection.

Related Topic