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
Vishnu7700Vishnu7700 

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

Can please help me to resolve error.

VF code

<apex:page controller="AsFour" sidebar="false" tabStyle="Account">

<apex:form >

<apex:sectionHeader title="AssignmentFour"/>

<apex:pageBlock >

<apex:pageBlockSection title="Search">

<apex:inputText value="{!searchString}" label="Search"/>

<apex:commandButton value="Search" action="{!doSearch}" reRender="output"/>

</apex:pageBlockSection>

<apex:outputPanel id="output">

<apex:pageBlockSection title="Results">

<apex:pageBlockTable value="{!accounts}" var="a">

<apex:column value="{!a.name}"/>

<apex:column value="{!a.BillingState}"/>

<apex:column value="{!a.Phone}"/>

<apex:column value="{!a.Webiste}"/>

</apex:pageBlockTable>

</apex:pageBlockSection>

</apex:outputPanel>

</apex:pageBlock>

</apex:form>

</apex:page>

 

Controller class

public class AsFour {

String Accountqurey;
public list<Account> listAccount{ get; set; }
public String searchString { get; set; }
public String errMsg;

public PageReference doSearch() {
listAccount = new list<Account>();
if(searchString !='' && searchString !=null){
for (List<Account> listAccount : [Select Id,Name,BillingState,Phone,Website FROM Account where name =:'searchString']) {
if(listAccount.size()>0 && listAccount.size()!= null){
}
else{
ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.Warning,'No Accounts Found');
ApexPages.addMessage(errMsg);
}
}
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Hello,

 

You are using accountsin the table

<apex:pageBlockTable value="{!accounts}" var="a">

 

But in controller you are fetching in this listAccount list.

So you must iterate thsi list in the table to get the values.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

All Answers

souvik9086souvik9086

Do it like this

 

public class AsFour {

String Accountqurey;
public list<Account> listAccount{ get; set; }
public String searchString { get; set; }
public String errMsg;

public PageReference doSearch() {
listAccount = new list<Account>();
if(searchString !='' && searchString !=null){
for (List<Account> listAccount : [Select Id,Name,BillingState,Phone,Website FROM Account where name =:'searchString']) {
if(listAccount.size()>0 && listAccount.size()!= null){
}
else{
ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.Warning,'No Accounts Found');
ApexPages.addMessage(errMsg);
}
}
}

return NULL;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Vishnu7700Vishnu7700

Thanks for reply.But if tired to search<United Oil & Gas Corp> a account from VF page unable to retrieve account details in Output panel. Any help is highly appreciated

public class AsFour {
List<Account> accounts;
String Accountqurey;
public list<Account> listAccount{ get; set; }
public String searchString { get; set; }
public String errMsg;

public List<Account> getAccounts() {
return accounts;
}

public PageReference doSearch() {
listAccount = new list<Account>();
if(searchString !='' && searchString !=null){
for (List<Account> listAccount : [Select Id,Name,BillingState,Phone,Website FROM Account where name =:'searchString']) {
if(listAccount.size()>0 && listAccount.size()!= null){
}
else{
ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.Warning,'No Accounts Found');
ApexPages.addMessage(errMsg);
}
}
}
return NULL;
}

souvik9086souvik9086

Hello,

 

You are using accountsin the table

<apex:pageBlockTable value="{!accounts}" var="a">

 

But in controller you are fetching in this listAccount list.

So you must iterate thsi list in the table to get the values.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

This was selected as the best answer
Sfd developerSfd developer

Hi Vishnu,

 

Actually the problem is your query. In your scenario, you have to use dynamic soql. Try this

 

public class AsFour {
List<Account> accounts;
String Accountqurey;
public list<Account> listAccount{ get; set; }
public String searchString { get; set; }
public String errMsg;
public List<Account> getAccounts() {
return accounts;
}
public PageReference doSearch() {
listAccount = new list<Account>();
if(searchString !='' && searchString !=null){
String searchQuery = 'Select Id,Name,BillingState,Phone,Website FROM Account where name = \'+searchString+\'';
for (List<Account> listAccount : Database.query(searchQuery)) {
if(listAccount.size()>0 && listAccount.size()!= null){
}
else{
ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.Warning,'No Accounts Found');
ApexPages.addMessage(errMsg);
}
}
}
return NULL;
}
}

 

Thanks,

Vishnu7700Vishnu7700

Hi Souvik,

Thanks for solution..

Here is a qucik question like in my VF page there are two pageblock section 1 for Search Text and search button and second one is to show results.

Here in the first section i'm faceing a long gap between search Text and button

Please find VF code

<apex:page controller="AsFour" sidebar="false" tabStyle="Account">
<apex:form >
<apex:pageMessages ></apex:pageMessages>
<apex:sectionHeader title="AsFour"/>
<apex:pageBlock >
<apex:pageBlockSection title="Search">
<apex:pageBlockSectionItem >
<apex:outputText value="Search"></apex:outputText>
<apex:inputText label="Search" value="{!searchString}"/>
</apex:pageBlockSectionItem>
<apex:commandButton value="Search" action="{!doSearch}" reRender="output,msg"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock >
<apex:outputPanel id="output">
<apex:pageBlockSection title="Results">
<apex:pageBlockTable value="{!accounts}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.BillingState}"/>
<apex:column value="{!a.Phone}"/>
<apex:column value="{!a.Website}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

souvik9086souvik9086

Try to keep

<apex:commandButton value="Search" action="{!doSearch}" reRender="output,msg"/> 

 

within

<apex:pageBlockSectionItem>

 

and test what is coming.

 

If the post helps you, please throw KUDOS.

Thanks

Vishnu7700Vishnu7700

Tried as u said but throws error that <apex:pageBlockSectionItem> component can have only 2 child components.

 

Here i blocked actually.Any idea pls.

souvik9086souvik9086

Then you can do like this

 

<apex:pageBlockSection title="Search" column="2">

<apex:outputText value="Search"></apex:outputText>
<apex:inputText label="Search" value="{!searchString}"/> 
<apex:commandButton value="Search" action="{!doSearch}" reRender="output,msg"/> 
</apex:pageBlockSection>

 

If this post is helpful please throw Kudos.

Thanks