+ Start a Discussion

Searching contacts based on lastname, firstname


I created a Visualforce page (on a custom object related to Account) where users will be able to search for contacts based on FirstName and LastName. When I enter the LastName and click Search, the page retrieves only the contacts based on LastName and works as expected. But when I enter FirstName and click Search, the page shows an error message ("Please enter firstname or lastname") which I created when the user does not enter anything in any of the fields and clicks Search. But it shows up even when FirstName is entered. I am not sure what is incorrect in the soql statement or in other area of the code. When I try to add another Search attribute based on Division, I get the same error message. Below is the partial Class code and Vf code which involves Firstname/Lastname search features. I appreciate any help in this regard. Thank you.

Class code:

public class AccountContacts{
public AccountContacts() {}
String aid=ApexPages.currentPage().getParameters().get('id');
 /* Variable declarations */
public List<cContact> contactList {get; set;}                              
public Boolean selectAllCheckbox {get; set;}                                  // Stores checkbox data.
public boolean errormsg=false;
public boolean errormsg1=false;

String userinput1;                                                             // Contact firstname
String userinp;                                                                  // Contact lastname

Public List<Contact> results = new List<Contact>();                            // Contact search results.
Public List<Contact> selectedContactsstep2 = new List<Contact>();
public List<Account_Contacts_Teams__c> act = new List<Account_Contacts_Teams__c>();
/* End of Variable declarations */

/* Getter and setter methods for getting the user input ie. Contact firstname and lastname from the UI */
public String getuserinput1(){return userinput1;}
public void setuserinput1(String userinp){this.userinput1=userinput1;}
public String getuserinp(){return userinp;}
public void setuserinp(String userinp){this.userinp=userinp;}
/*End of Getter and Setter methods */

/* Method to Search the contact database to fetch the query results */
public List<cContact> contactsearch()
    contactList=new List<cContact>();
    if((userinput1==null || userinput1=='') && (userinp==null || userinp==''))
     for(Contact c : [select Id,FirstName,LastName,Email,Phone,Title,Company__c,Division__c from Contact where FirstName like userinput1+'%' ])
         contactList.add(new cContact(c));
     for(Contact c : [select Id,FirstName,LastName,Email,Phone,Title,Company__c,Division__c from Contact where LastName like :userinp+'%'])
         contactList.add(new cContact(c));
   return null;

public List<cContact> getresults() {

return contactList;


VF code:

<apex:page controller="AccountContacts" tabStyle="Account">
    <apex:form >
      <apex:sectionHeader title="Step 1" subtitle="Select Contacts to Add to the Account"/>
             <apex:pageblock >
                <apex:pageBlockSection title="Search Contacts" columns="1"></apex:pageBlockSection>
                  <apex:panelGrid columns="2">
                 <apex:outputLabel style="font-weight:bold;" value="Contact FirstName"></apex:outputLabel>
                 <apex:inputText value="{!userinput1}" />
                 <apex:outputLabel style="font-weight:bold;" value="Contact LastName" ></apex:outputLabel>
                 <apex:inputText value="{!userinp}"/>
             <apex:commandButton value="Search" action="{!contactsearch}"/>
<!-- Display error message -->
<apex:pagemessage strength="2" title="Error!!" severity="error" detail="Please select a contact or enter different contact name to proceed" rendered="{!errormsg}"/>
<!-- End of error message -->
<!-- Display error message -->
<apex:pagemessage strength="2" title="Invalid!!" severity="error" detail="Please enter firstname or lastname" rendered="{!errormsg1}"/>
<!-- End of error message -->    

<!-- Display search results -->
<apex:pageblocksection columns="1" title="Search Results" rendered="{!NOT(ISNULL(results))}" >
  <apex:outputpanel id="Contactlist">

        <apex:pageBlockTable value="{!results}" var="contacts">
             <apex:column >
                <apex:facet name="header">
                    <apex:inputCheckbox onclick="checkAll(this)"/>
             <apex:inputCheckbox value="{!Contacts.selected}" id="selectLine1"/>       
            <apex:column headervalue="Contact FirstName">
                <apex:outputtext value="{!Contacts.con.FirstName}"/>
            <apex:column headervalue="Contact LastName">
                <apex:outputtext value="{!Contacts.con.LastName}"/>
            <apex:column headervalue="Title">
                <apex:outputtext value="{!Contacts.con.Title}"/>
            <apex:column headervalue="Phone">
                <apex:outputtext value="{!Contacts.con.Phone}"/>
            <apex:column headervalue="Email">
                <apex:outputtext value="{!Contacts.con.Email}"/>
            <apex:column headervalue="Company">
                <apex:outputtext value="{!Contacts.con.Company__c}"/>
            <apex:column headervalue="Division">
                <apex:outputtext value="{!Contacts.con.Division__c}"/>
          </apex:pageBlockTable>  <br/><br/>
can you modify below condition and try.

if((userinput1==null || userinput1=='') && (userinp==null || userinp==''))

Use below statement, the above condition true even the input value avaiable. try this and let me know. You can include firstname and lastname search critieria in single soql statement instead of two statements. For example firstname like '%input1%' or lastname like '%input2%'
if(userinput1==null && userinp==null )
Hi Nbk - Thanks for replying to my question. I tried with the above condition, but it did not work. All the names get retrieved unrelated to what is entered in FirstName. Also because the new condition does not check for blank fields, when I click search without entering anything in Firstname and Lastname fields, the error message does not get displayed either.