• narr
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi,

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>();
        errormsg=false;
    if((userinput1==null || userinput1=='') && (userinp==null || userinp==''))
    
     errormsg1=true;
    else
        errormsg1=false;
    if(!errormsg1){
          
       if(userinput1!=null){
     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));
     }
        }
        
       if(userinp!=null){
     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:panelGrid>
             <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:facet>
             <apex:inputCheckbox value="{!Contacts.selected}" id="selectLine1"/>       
            </apex:column>
            <apex:column headervalue="Contact FirstName">
                <apex:outputtext value="{!Contacts.con.FirstName}"/>
            </apex:column>
            <apex:column headervalue="Contact LastName">
                <apex:outputtext value="{!Contacts.con.LastName}"/>
            </apex:column>
            <apex:column headervalue="Title">
                <apex:outputtext value="{!Contacts.con.Title}"/>
            </apex:column>
            <apex:column headervalue="Phone">
                <apex:outputtext value="{!Contacts.con.Phone}"/>
            </apex:column>
            <apex:column headervalue="Email">
                <apex:outputtext value="{!Contacts.con.Email}"/>
            </apex:column>
            <apex:column headervalue="Company">
                <apex:outputtext value="{!Contacts.con.Company__c}"/>
            </apex:column>
            <apex:column headervalue="Division">
                <apex:outputtext value="{!Contacts.con.Division__c}"/>
            </apex:column>
          </apex:pageBlockTable>  <br/><br/>
    </apex:outputpanel>
  • November 11, 2014
  • Like
  • 0
Hi,

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>();
        errormsg=false;
    if((userinput1==null || userinput1=='') && (userinp==null || userinp==''))
    
     errormsg1=true;
    else
        errormsg1=false;
    if(!errormsg1){
          
       if(userinput1!=null){
     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));
     }
        }
        
       if(userinp!=null){
     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:panelGrid>
             <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:facet>
             <apex:inputCheckbox value="{!Contacts.selected}" id="selectLine1"/>       
            </apex:column>
            <apex:column headervalue="Contact FirstName">
                <apex:outputtext value="{!Contacts.con.FirstName}"/>
            </apex:column>
            <apex:column headervalue="Contact LastName">
                <apex:outputtext value="{!Contacts.con.LastName}"/>
            </apex:column>
            <apex:column headervalue="Title">
                <apex:outputtext value="{!Contacts.con.Title}"/>
            </apex:column>
            <apex:column headervalue="Phone">
                <apex:outputtext value="{!Contacts.con.Phone}"/>
            </apex:column>
            <apex:column headervalue="Email">
                <apex:outputtext value="{!Contacts.con.Email}"/>
            </apex:column>
            <apex:column headervalue="Company">
                <apex:outputtext value="{!Contacts.con.Company__c}"/>
            </apex:column>
            <apex:column headervalue="Division">
                <apex:outputtext value="{!Contacts.con.Division__c}"/>
            </apex:column>
          </apex:pageBlockTable>  <br/><br/>
    </apex:outputpanel>
  • November 11, 2014
  • Like
  • 0