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
chaitanya motupalli 13chaitanya motupalli 13 

missing return statement required return type

Hi I have written below apex method for LWC but i am getting "missing return statement required return type" error when trying to deploy to org.
please help me where i am doing wrong here.

public with sharing class bikeSearchResultController {
@AuraEnabled(cacheable=true)
public static list<Car__c> cardetailsapexmethod(string cartypeid){
    try {
        if(String.isEmpty(cartypeid)){
           return [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                   FROM Car__c];
        }else 
        if(!String.isEmpty(cartypeid)){
            return [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                    FROM Car__c
                    WHERE Car_Type__c=:cartypeid];
        }
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }
    
}
}

 
Best Answer chosen by chaitanya motupalli 13
Maharajan CMaharajan C
Hi,

Try the below changes:
 
public with sharing class bikeSearchResultController {
@AuraEnabled(cacheable=true)
public static list<Car__c> cardetailsapexmethod(string cartypeid){
    try {
        list<Car__c> carList = new list<Car__c>();
        if(String.isEmpty(cartypeid)){
           carList = [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                   FROM Car__c];
        }
	    else if(!String.isEmpty(cartypeid)){
           carList = [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                    FROM Car__c
                    WHERE Car_Type__c=:cartypeid];
        }
	return carList;
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }
    
}
}

Thanks,
Maharajan.C


 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Chaitanya,

Can you try if you are able to return after assigning the response of the soql to a list variable and returning the list variable like changing the above code to below:
 
list<car__c> clist= [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                   FROM Car__c];
 return clist;

Looking forward to your response.

Thanks.
Maharajan CMaharajan C
Hi,

Try the below changes:
 
public with sharing class bikeSearchResultController {
@AuraEnabled(cacheable=true)
public static list<Car__c> cardetailsapexmethod(string cartypeid){
    try {
        list<Car__c> carList = new list<Car__c>();
        if(String.isEmpty(cartypeid)){
           carList = [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                   FROM Car__c];
        }
	    else if(!String.isEmpty(cartypeid)){
           carList = [SELECT Id,Available_For_Rent__c,Build_Year__c,Car_Type__c,Name,Picture__c
                    FROM Car__c
                    WHERE Car_Type__c=:cartypeid];
        }
	return carList;
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }
    
}
}

Thanks,
Maharajan.C


 
This was selected as the best answer