You need to sign in to do that
Don't have an account?
Suma
Dynamic search with apex and visualforce. Need help in displaying error message in VF page.
I am trying to search Contacts details from database using lastname field and dsiplay the table in VF page. I am able to search and display the existing records in the page but if the record(Contact) is not in the database, I need to display the error message: "Contact could not found". This is where i am looking for some help.
Code for my controller is:
public with sharing class ContactSearchController
{
// Global variables
public List<Contact> contacts{get;set;}
public String searchString{get;set;}
// Constructor for stand alone page
public ContactSearchController()
{
searchString = '';
doSearch();
}
// Methods for getting List of Contacts
public void doSearch()
{
String queryString = '';
if(searchString!= null)
{
queryString = '%'+searchString+'%';
contacts = [SELECT id, name, accountId, email, phone
FROM contact C
WHERE lastname LIKE :queryString
LIMIT 10];
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact could not found!'));
}
}
}
My VF page is :
<apex:page controller="ContactSearchController" >
<apex:form >
<!-- actionFunction is for calling Controller's method -->
<apex:actionFunction action="{!doSearch}" name="apexdoSearch" reRender="myData,errors"/>
<!-- Filter Section -->
<label> <b>Filter by Last Name:</b></label>
<apex:inputText value="{!searchString}" onkeyup="apexdoSearch()"/>
<br></BR>
<!-- My Page Block -->
<apex:pageBlock title="Find Customer" id="myData">
<apex:pageMessages id="errors"/>
<apex:pageBlockTable value="{!contacts}" var="contact">
<apex:column value="{!contact.name}"/>
<apex:column value="{!contact.accountid}"/>
<apex:column value="{!contact.email}"/>
<apex:column value="{!contact.phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Code for my controller is:
public with sharing class ContactSearchController
{
// Global variables
public List<Contact> contacts{get;set;}
public String searchString{get;set;}
// Constructor for stand alone page
public ContactSearchController()
{
searchString = '';
doSearch();
}
// Methods for getting List of Contacts
public void doSearch()
{
String queryString = '';
if(searchString!= null)
{
queryString = '%'+searchString+'%';
contacts = [SELECT id, name, accountId, email, phone
FROM contact C
WHERE lastname LIKE :queryString
LIMIT 10];
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact could not found!'));
}
}
}
My VF page is :
<apex:page controller="ContactSearchController" >
<apex:form >
<!-- actionFunction is for calling Controller's method -->
<apex:actionFunction action="{!doSearch}" name="apexdoSearch" reRender="myData,errors"/>
<!-- Filter Section -->
<label> <b>Filter by Last Name:</b></label>
<apex:inputText value="{!searchString}" onkeyup="apexdoSearch()"/>
<br></BR>
<!-- My Page Block -->
<apex:pageBlock title="Find Customer" id="myData">
<apex:pageMessages id="errors"/>
<apex:pageBlockTable value="{!contacts}" var="contact">
<apex:column value="{!contact.name}"/>
<apex:column value="{!contact.accountid}"/>
<apex:column value="{!contact.email}"/>
<apex:column value="{!contact.phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
All Answers