+ Start a Discussion
PzyrenPzyren 

Error - Non void method might not return a value

I keep getting the above error when I try to compile. I'm not sure if my placement of return values are off or not. Anyone see what I should be doing differently?

 

 

public List<Task> filterRecords(){
  	actList = new List<Task>();
  	
  	try {
  	actList = [
  	          SELECT Subject, WhatId, activityDate, status, priority, category__c, type__c, whoId
  	          FROM Task 
  	          WHERE activityDate=:act.ActivityDate AND status=:act.Status AND category__c=:act.category__c 
  	               AND type__c=:act.type__c AND priority=:act.Priority
  	          LIMIT 200
  	          ];
  	          
  	 if(actList.isEmpty()){
  	 	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Could not find any records'));
  	 	return null;
  	 } 
  	 else if(!actList.isEmpty()){
  	 	return actList;
  	 } 
  	}
  	catch(Exception e){System.debug(e.getMessage());}
  }

 

Best Answer chosen by Admin (Salesforce Developers) 
liron169liron169

Hello,

 

If you have exception than the code go to the 'Catch' section and after the that the method will not return nothing.

This is the error.

 

The return should be at the end.

Also it's good programming to have 1 return statement in function.

 

 

 

public List<Task> filterRecords(){
  	actList = new List<Task>();
  	
  	try {
  	actList = [
  	          SELECT Subject, WhatId, activityDate, status, priority, category__c, type__c, whoId
  	          FROM Task 
  	          WHERE activityDate=:act.ActivityDate AND status=:act.Status AND category__c=:act.category__c 
  	               AND type__c=:act.type__c AND priority=:act.Priority
  	          LIMIT 200
  	          ];
  	          
  	 if(actList.isEmpty()){
  	 	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Could not find any records'));
actList=null;
} } catch(Exception e){System.debug(e.getMessage());}

return actList; }

All Answers

liron169liron169

Hello,

 

If you have exception than the code go to the 'Catch' section and after the that the method will not return nothing.

This is the error.

 

The return should be at the end.

Also it's good programming to have 1 return statement in function.

 

 

 

public List<Task> filterRecords(){
  	actList = new List<Task>();
  	
  	try {
  	actList = [
  	          SELECT Subject, WhatId, activityDate, status, priority, category__c, type__c, whoId
  	          FROM Task 
  	          WHERE activityDate=:act.ActivityDate AND status=:act.Status AND category__c=:act.category__c 
  	               AND type__c=:act.type__c AND priority=:act.Priority
  	          LIMIT 200
  	          ];
  	          
  	 if(actList.isEmpty()){
  	 	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Could not find any records'));
actList=null;
} } catch(Exception e){System.debug(e.getMessage());}

return actList; }

This was selected as the best answer
pooja@magichorse.compooja@magichorse.com

Hi,

 

Try this one.

public List<Task> filterRecords(){
  	actList = new List<Task>();
  	
  	try {
  	actList = [
  	          SELECT Subject, WhatId, activityDate, status, priority, category__c, type__c, whoId
  	          FROM Task 
  	          WHERE activityDate=:act.ActivityDate AND status=:act.Status AND category__c=:act.category__c 
  	               AND type__c=:act.type__c AND priority=:act.Priority
  	          LIMIT 200
  	          ];
  	          
  	 if(actList.isEmpty()){
  	 	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Could not find any records'));
  	 	return null;
  	 } 
  	 else if(!actList.isEmpty()){
  	 	return actList;
  	 } 
  	}
  	catch(Exception e){
            System.debug(e.getMessage());
            return null;
        }
  }

 

gautam_singhgautam_singh

Hi,

 

Try this piece of Code written below. It works correctly . You have declared the method as public List<Task> filterRecords(){}
which means that it will surely return some value at the last. You have correctly returned the values from the respective clauses but at the end it should return a value due to which it is throwing the error.

 

Try the piece of code below , it fixes the error.



Important :

Click on the Star Icon aside if this post provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You


public class checkoneproblem {
    
    public List<task> actList {get;set;}
    public List<Task> filterRecords(){
        actList = new List<Task>();
        try {
            actList = [SELECT Subject, WhatId, activityDate, status, priority, whoId FROM Task LIMIT 200 ];
            if(actList.isEmpty()){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Could not find any records'));
                return null;
             }else if(!actList.isEmpty()){
                return actList;
             } 
            }
        catch(Exception e){System.debug(e.getMessage());}
      return actList;
      }
      
}

 


asish1989asish1989
public List<Task> filterRecords(){
  	actList = new List<Task>();
  	
  	try {
			actList = [
					  SELECT Subject, WhatId, activityDate, status, priority, category__c, type__c, whoId
					  FROM Task 
					  WHERE activityDate=:act.ActivityDate AND status=:act.Status AND category__c=:act.category__c 
						   AND type__c=:act.type__c AND priority=:act.Priority
					  LIMIT 200
					  ];
					  
			 if(actList.isEmpty()){
				ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Could not find any records'));
				return null;
			 } 
		  
  	}
  	catch(Exception e){
		System.debug(e.getMessage());
	}
	return actList;
  }

 

PzyrenPzyren
Thanks everybody. I realized my mistake as soon as I saw you guys point it out. Cheers mates. KCCO!