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
venkatesh 44venkatesh 44 

Non-void method might not return a value or might have statement after a return statement.

Hi am very new to apex am writing small program "fetch the account name from the account records by passing phone numer" am geting this erroe

 

Non-void method might not return a value or might have statement after a return statement. at line 7 column 1

 

my program

 

public class Fetch_Account_Name_From_Phone
{
public set<string>Fetch_Acc_Name(String p){
set<String> s1=new set<String>();

List<Account> acc=[select name from Account ];
for(Account a:acc)
{
String s=a.name;
s1.add(s);
System.debug('0019000000Btg2c'+s1);
return s1;

}
}
}

 

please can any one tel me how can i solve this problem

Best Answer chosen by Admin (Salesforce Developers) 
Jia HuJia Hu

try this,
public class Fetch_Account_Name_From_Phone
{
public set<string>Fetch_Acc_Name(String p){
set<String> s1=new set<String>();
List<Account> acc=[select name from Account ];
for(Account a:acc)
{
String s=a.name;
s1.add(s);
System.debug('0019000000Btg2c'+s1);
}
return s1;  // put this outside your for(Account a:acc) loop
}
}