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
Carolyn juliana 7Carolyn juliana 7 

getting Illegal conversion from List<AggregateResult> to List<String>

Hi,
I am tryin gto fetch a field value and below is the code,but i am getting below error in SForce
 
Illegal conversion from List<AggregateResult> to List<String>

here is apex code
public class PickListHandler {
    @AuraEnabled
    public static List<String> getLevel1(){
     
    
       List<AggregateResult> groupedLevel1
  = [select Level_1__c,COUNT(id) from Case_Type_Data__c  group by Level_1__c];
        
        for(AggregateResult  ar : groupedLevel1){                
           System.debug('Level Value Is' + ar.get('Level_1__c'));
        
        }
      
        return groupedLevel1;
    } 
   

}

 
Best Answer chosen by Carolyn juliana 7
Andrew GAndrew G
Your method is expecting to pass back a list of Strings,
public static List<String> getLevel1(){
but you are passing back a list of Aggregate results.  
return groupedLevel1;
The error occurs because an List of AggregateResult is not the same as a List of Strings.

Either change the method to pass back a List<AggregateResult> or modify the method to convert the AggregateResult to a List of Strings.

Note, if you pass back the List<AggregateResult> , you will need to handle it as such in the code that is invoking this method.

Regards
Andrew
 

All Answers

Andrew GAndrew G
Your method is expecting to pass back a list of Strings,
public static List<String> getLevel1(){
but you are passing back a list of Aggregate results.  
return groupedLevel1;
The error occurs because an List of AggregateResult is not the same as a List of Strings.

Either change the method to pass back a List<AggregateResult> or modify the method to convert the AggregateResult to a List of Strings.

Note, if you pass back the List<AggregateResult> , you will need to handle it as such in the code that is invoking this method.

Regards
Andrew
 
This was selected as the best answer
Rahul Shambhwani 8Rahul Shambhwani 8
Right as per @Andrew G
Your return type of the method is 
List<String>
So you can try::
List<String> tempLst = new List<String>()
 for(AggregateResult  ar : groupedLevel1){
    tempLst.add(ar.get('Level_1__c'));
    System.debug('Level Value Is' + ar.get('Level_1__c'));
 }

 return tempLst;