2

I have trouble with a component in an Experience site. I'm doing a search bar, which makes a search in the information of two components. It has to show the coincidence or a message like "there's no a coincide". I put the coincidence found in a list so, I'm trying to make a conditional with if but I have an error.

@AuraEnabled
    public static List<Beverage__c> showMessage (String searchKey) {
        String name = '%' + searchKey + '%';
        List <Beverage__c> findCoffee = [SELECT Id, Name, Status__c FROM Beverage__c WHERE Name LIKE :Name AND Type__c = 'Hot'];
        List <String> msg = new List <String> {'We do not have this coffee'};
        if(findCoffee.isEmpty()) {
            return msg;
        } else {
            return findCoffee;
        }
    }

And the error says: Illegal conversion from List to List<Beverage__c>

I hope somebody could help me. Thank you :D

I've tried...

public class Result  {
        @AuraEnabled public List<Beverage__c> beverages { get; set;}
        @AuraEnabled public String message { get; set;}
        public Result(){
        beverages = new List<Beverage__c>();
        message = '';
        }
    } 
    
    @AuraEnabled
    public static List<Beverage__c> showMessage (String searchKey) {
        String name = '%' + searchKey + '%';
        List <Beverage__c> findCoffee = [SELECT Id, Name, Status__c FROM Beverage__c WHERE Name LIKE :Name AND Type__c = 'Hot'];
        Result r = new Result();
        if(findCoffee.isEmpty()) {
            r.message = 'We do not have this coffee';
        } else {
            r.beverages = findCoffee;
        }
        return r;
    }
}

And I got the error Illegal conversion from HotCoffeeListController.Result to List<Beverage__c>

2
  • After your update of trying Caspar's answer, you need to change your main method to return a Result rather than a List<Beverage__c>, then parse the Result on the calling end.
    – Moonpie
    Commented Apr 26, 2022 at 0:53
  • P.S. Thanks & good job for adding your update of what you've tried to the *end of your question as an addendum, rather than changing your original question.* :thumbsup:
    – Moonpie
    Commented Apr 26, 2022 at 1:01

1 Answer 1

2

Because Apex is strongly typed, you have to return the type of object that your method specifies - in this case a List<Beverage__c>

You can either return an empty beverage list ie:

return new List<Beverage__c>();

Or create a return object that contains the list and an error message.

Declare your inner class (can be inside the main class you have above):

public class Result  {
  @AuraEnabled public List<Beverage__c> beverages { get; set; }
  @AuraEnabled public String message { get; set; }
  public Result(){
    beverages = new List<Beverage__c>();
    message = '';
  }
} 

Then you code in showMessage would have this at the end:

Result r = new Result();

if (findCoffee.isEmpty()){
  r.message = 'We do not have this coffee';
}
else {
  r.beverages = findCoffee;
}
return r;
4
  • The Result wrapper class reads a lot nicer in the long run. I definitely recommend some kind of structured data.
    – sfdcfox
    Commented Apr 25, 2022 at 23:49
  • 1
    P.S. You forgot the AuraEnabled bits 😁
    – sfdcfox
    Commented Apr 25, 2022 at 23:50
  • I'm learning all this, and I'm pretty sure that I'm doing something wrong, because it shows another error Commented Apr 26, 2022 at 0:16
  • What is the error? Also, look up how to construct an inner class. Oh and it might be a good idea to check if AuraEnabled methods can access inner classes ( I know at one point they couldn't ). If this is the case, you'll need a standalone results class (it will look the same though, code-wise) Commented Apr 26, 2022 at 0:20

You must log in to answer this question.

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