function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vandana Rattan 4Vandana Rattan 4 

Use @invocableMethod in Flows

Hi expperts,

Need suggestions on below use case:

I have to create a button on Account Detail page to open a screen with a dropdown having names of associated Contact Roles. AccountContactRoles object is not customizable so I was thinking of creating a flow which would use an apex method defined using @invocableMethod annotation to find associated Contact Role names. Issues:

1) I created a collection variable to hold the output from the method. But I am not able to select that from Target dropdown. If I create new Collection variable I get error as "DataType of resource you entered does not match. Am I missing something here?Error screenshot"

2) How can dropdown (choice or dynamic choice) in a screen be populated using these values?

OR should I rather concentrate on Visualforce page?

Thanks in Advance,
Vandana
KaranrajKaranraj
Vandana - What is the data type of the variable output in your apex class? Same data type you have to assign it to the 'Target' section. If its returning list of account record then create sobject variable in the Target section and then assign. If there is mismatch in the data type then it will accept. 
Vandana Rattan 4Vandana Rattan 4
Hi Karanraj,

Thank you for the reply. I am returning a List of Strings from my apex class and trying to create an sObject of Text dataType.
njo103njo103
Hello Vandana,

We are encountering the same issue.  

Did you find the solution?
Chad BarbourChad Barbour
I had a similar need recently and encountered the same issue. I found a solution that worked for me and thought I would share.

If the expectation is to receive a collection you can use in the flow, you'll need to have your method return a collection of collections. In this case, if you're wanting a list of strings (List<String>), you'll want to have it return a list containing lists of strings (List<List<String>>). Here's a brief example...
 
public static List<List<String>> getListOfStrings(){
    
    // This will be your working list
    List<String> stringList = new List<String>(); 

    // This will be your "wrapper" list
    List<List<String>> listOfStringLists = new List<List<Date>>();  

    // Add code to populate stringList

    listOfStringLists.add(stringList);
    
    return listOfStringLists;

}

For your test class, you'll need to reverse this process to get the list of strings...
@isTest static void testGetListOfStrings(){

    // This will be your list of lists returned from the method
    List<List<String>> listOfTestStringLists = getListofStrings();

    // This will be the extracted list used for testing (using the list at index 0)
    List<String> testStringList = getListOfString.get(0);

    // A simple check to confirm the list isn't empty
    // Update accordingly for your needs
    system.assert(!testStringList.isEmpty());

}