1

I'm looking to provide a Flow User with a list of Search Results based on a query. I have the following set of code, and haven't been able to figure out how to get it to work. (I've tried moving the classes to their own files, adding them to a Permission Set assigned to the running user).

Once the code below is in an org, I can create a flow that calls the action (say passing an argument of Banana). I can then make a Loop element that loops over results.

My error occurs when I try to reference anything within the Fruit class. I get the error: The "name" field doesn't exist on the "ComplexAction$Fruit" object, or you don't have access to the field.

Any tips on making this work? I have seen this SO post, though my error happens after the screens in the post. How to iterate over List<List<String>> which is returned by Apex, in a flow?

global with sharing class ComplexAction {
  global class Basket { // this is our group of search results
    @InvocableVariable(label='Original Query')
    global String origQuery;
    @InvocableVariable(label='Results')
    global List<Fruit> results;
  }

  global class Fruit {
    @InvocableVariable(label='Fruit Name')
    global String name;
  }

  @InvocableMethod(label='Search for Fruit')
  global static List<Basket> searchForFruit(List<String> criteria) {
    //imagine code that does something useful
    return null; //this doesn't matter for the purpose of this post
  }
}
3
  • 2
    1) Where are you trying to "reference anything within the Fruit class" (i.e., where are you getting the error)? In a Flow? Inside your searchForFruit() method? 2) For clarification, is it a copying/pasting to SFSE error, or does your error state ComplexActions? Notice that has an s but your class name does not, so I'm not sure how you are referencing it. It seems to me that some additional code and/or Flow details need to be shared.
    – Moonpie
    Commented May 23, 2022 at 12:10
  • Ah, ComplexActions$Fruit is indeed a typo, I'll fix that in the post. I am getting my error in the flow, when I try to use the results. So in the flow, i have: - Action - Iterate Loop (looping through Action's results variable - Try to use Loop Var (which is a single result, of Fruit class). This is where I get the error where the Fruit variable name doesn't exist Commented May 24, 2022 at 3:11
  • 1
    Sounds like the problem might be with the collection variable data type or what ever you are performing inside the loop. You should add the flow screenshots/ details for better resolution.
    – arut
    Commented May 24, 2022 at 6:52

2 Answers 2

1
+100

Want to clarify the previous answer.

You can use this code, but need to create 2 separate classes

global with sharing class ComplexAction {
  global class Basket { // this is our group of search results
    @InvocableVariable(label='Original Query')
    global String origQuery;
    @InvocableVariable(label='Results')
    global List<Fruit> results;
  }
    
  @InvocableMethod(label='Search for Fruit')
  global static List<Basket> searchForFruit(List<String> criteria) {
    //imagine code that does something useful
    return null; //this doesn't matter for the purpose of this post
  }
}

and

global class Fruit {
    @AuraEnabled
    global String name {get; set;}
}

Fruit you can specify as Apex-Defined variable

enter image description here

And inside for Loop put Assignment Action first where assign Loop Current item to your var

enter image description here

PS: Other annotations can’t be used with the InvocableVariable annotation - from documentation. Thanks

1
  • This answer got me the closest. I was able to leverage the Fruit.name property as I expected! Commented May 25, 2022 at 2:41
1

I took your code and created a Fruit class on its own file, and just added the @AuraEnabled annotation to it, as suggested by salesforce when you are creating an Apex-Defined variable:

enter image description here

global class Fruit {
    @InvocableVariable
    @AuraEnabled
    global String name;
}

and on the Invocable method, I'm returning a list of list of Fruit, it looks like this:

global with sharing class ComplexAction {
    @InvocableMethod(label='Search for Fruit')
    global static List<List<Fruit>> searchForFruit(List<String> criteria) {
        List<List<Fruit>> results = new List<List<Fruit>>();
        List<Fruit> fruits = new List<Fruit>();
        for (String fruitName : new List<String>{ 'Banana', 'Apple', 'Strawberry' }) {
            Fruit f = new Fruit();
            f.name = fruitName;
            fruits.add(f);
        }
        results.add(fruits);
        return results;
    }
}

and it seems to be working when looping that collection variable on the flow, hope this helps or let me know if you need further details

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .