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
Mayank Deshpande 2Mayank Deshpande 2 

Hi All, I am new in sales force. My requirement is like i want to search Contacts based on Last Name.

I Create a VF page with below Code 

apex:page Controller="ContactNomineeController" showHeader="True">
<apex:image value="{!$Resource.Contact}" width="500" height="100"/>
<apex:form >
            <apex:actionFunction action="{doquery}" name="QueryContact" reRender="MyContact"/>
            
            <div class="filterDiv">
            <label>Filter By Last Name:</label>
                       <apex:inputText value="{!serachstring}"  onkeyup="apexDoQuery()"/>
                       </div>
 <apex:pageBlock title="Search Contact" id="MyContact">
 <apex:pageBlockButtons >
 <apex:commandButton value="save" action="{!save}"/> 
 </apex:pageBlockButtons>
 <apex:pageBlockTable value="{!ConList}" var="Con">
<apex:column value="{!Con.Name}"/>
  <apex:column value="{!Con.accountid}"/>
<apex:column headerValue="Email"> 
   <apex:inputField value="{!Con.email}" />
  </apex:column> 
 <apex:column value="{!Con.phone}"/> 
  <apex:column headerValue="Nominee"> 
   <apex:inputCheckbox value="{!Con.Nominee__c}" />
  </apex:column> 
 </apex:pageBlockTable>
 </apex:pageBlock>
 </apex:form>
</apex:page>

and Controller Class 

public with sharing class ContactNomineeController {
 Public List<Contact> ConList {get;set;}
 Public List<Contact> Contacts {get;set;}
 Public String serachstring {get;set;}
 Public ContactNomineeController (){
   serachstring='';
   doquery();
 }
  Public void doquery(){
 
 String querystring='';
 
 if (serachstring!= null)
 querystring = '%' +serachstring+ '%';
 
 ConList= [select id,accountid,name,email,phone,Nominee__C from Contact where lastname like :querystring LIMIT 10 ];
 }
 Public void save() {
 update ConList; }
 
}

Code saved but not able to search contact.
 
Need help to understand the loophole.
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi,

Try the below code.It works for me
 
VF Page Code:
--------------------

<apex:page controller="SearchContact_AC">
    <apex:form>
        <apex:messages />
        <apex:pageBlock>
            Enter LastName:<apex:inputText value="{!Searchkey}"/>
            <apex:commandButton value="Search" action="{!Search}"/>
        </apex:pageBlock>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!cntlist}" var="cnt">
                <apex:column value="{!cnt.Name}"/>
                <apex:column value="{!cnt.AccountId}"/>
                <apex:column value="{!cnt.Email}"/>
                <apex:column value="{!cnt.Phone}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller Code:
----------------------
public class SearchContact_AC 
{
    public String Searchkey{get;set;}
    public List<Contact> cntlist{get;set;}
    
    public void Search()
    {
        cntlist=[select Name,AccountId,Email,Phone from Contact where LastName=:SearchKey];
        if(cntlist.size()==0)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.WARNING,'No Record Found'));
        }
    }
}

Hope it will satistify your requirement

Regards,
Vignesh.B
Mayank Deshpande 2Mayank Deshpande 2
Hi Vignesh
Thanks!
 but i tried to search the contact using apex:action function and for that i define do query method . but it only refersh the page. how can we solve this.