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
ShravanKumarBagamShravanKumarBagam 

Query on Error message display

 

Hi,

I want to display an error msg whenever invalid account id entered. please let me know how can i achieve it.

 

Here is code..

 

Class

 

public with sharing class asgn2cls {

public string SearchAccount {get; set;}

public List<Account> Account {get; set;}

public asgn2cls(ApexPages.StandardController Controller){

}

public pagereference Search(){

try{

if (SearchAccount != '' ){

if(SearchAccount != '')

Account = [select id, name,BillingState,phone,website from Account where name=:SearchAccount ];

}

else {

ApexPages.Message msg=new ApexPages.Message(ApexPages.Severity.Warning,'Please Enter a Search String' );
ApexPages.addMessage(msg);
}

}

catch(exception e){

ApexPages.Message msg=new ApexPages.Message(ApexPages.Severity.Fatal,'No Accounts Found');
ApexPages.addmessage(msg);

}
return null;

}



}

 

 

VF Page

 

<apex:page showHeader="false" standardController="Account" extensions="asgn2cls">

<apex:form >
<apex:pageBlock id="pb">

<apex:pageBlockSection title="Search" collapsible="false" columns="3" >
<apex:outputLabel style="margin-left:190px"><b>Search</b></apex:outputLabel>
<apex:inputText value="{!SearchAccount}" />
<apex:commandButton value="Search" action="{!Search}" reRender="pb" style="margin-right:800px" />
</apex:pageBlockSection>

<apex:pageBlockSection title="Results" collapsible="false"></apex:pageBlockSection><br/><br/>
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockTable value="{!Account}" var="a">
<apex:column headerValue="Account Name" value="{!a.name}"/>
<apex:column headerValue="BillingState" value="{!a.BillingState}"/>
<apex:column headerValue="Phone" value="{!a.Phone}"/>
<apex:column headerValue="Website" value="{!a.website}"/>
</apex:pageBlockTable>
</apex:pageBlock>


</apex:form>
</apex:page>

SamuelDeRyckeSamuelDeRycke

What is not happening ? Your code looks ok. You're adding pagemessages and have the <apex:pageMessages ></apex:pageMessages> element in your VF. Do you mean if no account is found ?

 

if(SearchAccount != '')
  Account = [select id, name,BillingState,phone,website from Account where name=:SearchAccount ];
}

 You have to keep in mind this can also return an empty list, or a list of multiple accounts (accounts may have the same name) so you'll get an exception in that case. I would put the query result in a list variable, and chek the list size. If <1= add page message that there were no results.


 

Besides that, 

 

if (SearchAccount != '') {
    if (SearchAccount != '') Account = [select id, name, BillingState, phone, website from Account where name = : SearchAccount];
} else {
    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Warning, 'Please Enter a Search String');
    ApexPages.addMessage(msg);
}

Why do that twice ?  once is enough ..