+ Start a Discussion
Justin DJustin D 

Compile Error: Missing return statement required return type

Hi Everyone,
I am trying to display data using Wrapper class.
I got this error.  Compile Error: Missing return statement required return type at line 15:
This is line 15: public List < TableRow2 > searchPatients(){​
Appreciated for any feedback!

This is Apex code:
------------------------------------------------------------------------------------------------
public class WrapperDemoController2{

    public String mrn {get;set;}  
 
    /*Wrapper Class*/
    public class TableRow2{
        public String sfprn {get;set;}
        public String mrn {get;set;}
        public Decimal SurveySelection {get;set;}
    }
       
    public List<TableRow2> searchPatients {get;set;}
       
    /*Method that has a return type of List<TableRow2>*/  
    public List<TableRow2> searchPatients(){

        searchPatients = new List<TableRow2>();
        TableRow2 tr;

        for(Patient__c con : [SELECT sfprn__c, 
                                    mrn__c, 
                                    (select SurveySelection__c.SurveySelection__c from SurveySelections__r) 
                                    FROM Patient__c
                                    where mrn__c =:mrn                                    
                                    ]){
                                 
            tr = new TableRow2();
            tr.sfprn = con.sfprn__c;
            tr.mrn = con.mrn__c;
            tr.SurveySelection = con.SurveySelections__r.isEmpty() ? null : con.SurveySelections__r[0].SurveySelection__c;
            
            searchPatients.add(tr);
            }
     }    
}

This is VF page:
---------------------------------------
<apex:page controller="WrapperDemoController2">
<apex:form >
    <apex:pageBlock >
        <!-- Serarch button-->
        <apex:pageBlockButtons location="top">
            <apex:commandButton value="Search" action="{!searchPatients}" reRender="contact-table"/>
        </apex:pageBlockButtons>
        
        <!-- Input -->
        <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="MRN" />
            <apex:inputText value="{!mrn}"/>
        
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    
        <!-- Output --->
        <apex:pageBlockTable value="{!searchPatients}" var="c">
            <apex:column value="{!c.mrn}"/>
            <apex:column value="{!c.sfprn}"/>
            <apex:column value="{!c.SurveySelection}"/>
        </apex:pageBlockTable>   
    </apex:pageBlock>
</apex:form>    
</apex:page>
Nithesh NNithesh N
You forgot to add return statement.... Try this
public class WrapperDemoController2{

    public String mrn {get;set;}  
 
    /*Wrapper Class*/
    public class TableRow2{
        public String sfprn {get;set;}
        public String mrn {get;set;}
        public Decimal SurveySelection {get;set;}
    }
       
    public List<TableRow2> searchPatients {get;set;}
       
    /*Method that has a return type of List<TableRow2>*/  
    public List<TableRow2> searchPatients(){

        searchPatients = new List<TableRow2>();
        TableRow2 tr;

        for(Patient__c con : [SELECT sfprn__c, 
                                    mrn__c, 
                                    (select SurveySelection__c.SurveySelection__c from SurveySelections__r) 
                                    FROM Patient__c
                                    where mrn__c =:mrn                                    
                                    ]){
                                 
            tr = new TableRow2();
            tr.sfprn = con.sfprn__c;
            tr.mrn = con.mrn__c;
            tr.SurveySelection = con.SurveySelections__r.isEmpty() ? null : con.SurveySelections__r[0].SurveySelection__c;
            
            searchPatients.add(tr);
            }
            
            return searchPatients;
            
     }    
}

Best,
Nithesh
jigarshahjigarshah
@Justin D Your searchPatients() method signature is defined to return a List of an inner class i.e. TableRow2. However, your method does not have any statement that returns a valid instance of List<TableRow2> type.

You can modify the searchPatients() method as follows. Incase you do not intend to return anything, just change the return type of the searchPatients() to void and your existing code would work. Additionally, I noticed, that there are no explicit LIMIT clauses which could potentially cause the code to fail in case there are more than 50,000 records returned which match the qualifying criteria.

Let me know if this is helpful and fixes the issue for you.
public List<TableRow2> searchPatients(){

        searchPatients = new List<TableRow2>();
        TableRow2 tr;

        for(Patient__c con : [SELECT sfprn__c, 
                                    mrn__c, 
                                    (select SurveySelection__c.SurveySelection__c from SurveySelections__r) 
                                    FROM Patient__c
                                    where mrn__c =:mrn                                    
                                    ]){
                                 
            tr = new TableRow2();
            tr.sfprn = con.sfprn__c;
            tr.mrn = con.mrn__c;
            tr.SurveySelection = con.SurveySelections__r.isEmpty() ? null : con.SurveySelections__r[0].SurveySelection__c;
            
            searchPatients.add(tr);
            }

           //Add this line of code to fix the issue.
           return searchPatients;
     }    
}
jigarshahjigarshah
I meant LIMIT clause in the SOQL query.