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
Vinay_guptaVinay_gupta 

Apply search filter on VF Page

Hi All,

I am new to Salesforce and need some urgent help to write logic for search filter on VF Controller.

I have two filer field i.e. Name and Email id on lookup VF page which display the list of users and it is being controlled by a controller.

Request you please suggest me a logic in such a way like if a user tries to search with Name, Email id or both. Then the list should populate result accordingly.

I want to put this in the 'Where' condition to fetch the result using string Soql.

Select name,emailid from User where name like '%XYZ%'  and EMaild like '%XYZ%';

Kindly suggest.
Lookup Filter on VF page


Regards,
Vinay
Best Answer chosen by Vinay_gupta
GhanshyamChoudhariGhanshyamChoudhari
Hi,
apex:
public with sharing class consearchcontrollerr { 
    public list <Contact> con {get;set;} 
    public string searchNamestring {get;set;} 
    public string searchEmailstring {get;set;} 
    public consearchcontrollerr() { 
    } 
    public void search(){         
        string searchquery='select id,FirstName,Email from Contact where FirstName like \'%'+searchNamestring+'%\' or Email like \'%'+searchEmailstring+'%\''; 
        system.debug('searchquery@@'+searchquery);
        con= Database.query(searchquery); 
    } 
    public void clear(){ 
        con.clear();  
    } 
}

VF
<apex:page Controller="consearchcontrollerr" > 
    <apex:form > 
        Contact Name
        <apex:inputText value="{!searchNamestring}" label="Input" /> 
        Contact Email
        <apex:inputText value="{!searchEmailstring}" label="Input"/> 
        <apex:commandButton value="Search records" action="{!search}"/> 
        <apex:commandButton value="Clear records" action="{!clear}"/> 
        <apex:pageBlock title="Search Result "> 
            <apex:pageblockTable value="{!con}" var="a"> 
                <apex:column value="{!a.FirstName}"/> 
                <apex:column value="{!a.Email}"/> 
            </apex:pageBlockTable> 
        </apex:pageBlock> 
    </apex:form> 
</apex:page>




Mark as best answer if it helps you.
Thanks,
Ghanshyam Choudhari

All Answers

GhanshyamChoudhariGhanshyamChoudhari
Hi,
apex:
public with sharing class consearchcontrollerr { 
    public list <Contact> con {get;set;} 
    public string searchNamestring {get;set;} 
    public string searchEmailstring {get;set;} 
    public consearchcontrollerr() { 
    } 
    public void search(){         
        string searchquery='select id,FirstName,Email from Contact where FirstName like \'%'+searchNamestring+'%\' or Email like \'%'+searchEmailstring+'%\''; 
        system.debug('searchquery@@'+searchquery);
        con= Database.query(searchquery); 
    } 
    public void clear(){ 
        con.clear();  
    } 
}

VF
<apex:page Controller="consearchcontrollerr" > 
    <apex:form > 
        Contact Name
        <apex:inputText value="{!searchNamestring}" label="Input" /> 
        Contact Email
        <apex:inputText value="{!searchEmailstring}" label="Input"/> 
        <apex:commandButton value="Search records" action="{!search}"/> 
        <apex:commandButton value="Clear records" action="{!clear}"/> 
        <apex:pageBlock title="Search Result "> 
            <apex:pageblockTable value="{!con}" var="a"> 
                <apex:column value="{!a.FirstName}"/> 
                <apex:column value="{!a.Email}"/> 
            </apex:pageBlockTable> 
        </apex:pageBlock> 
    </apex:form> 
</apex:page>




Mark as best answer if it helps you.
Thanks,
Ghanshyam Choudhari
This was selected as the best answer
Vinay_guptaVinay_gupta

Hi Ghanshyam,

Thanks for your Response.

I tried and it is not working as expected. Like if user enters only Firstname then it should filter with Firstname. Similarly, if a user enters only email id then it should search with email id. 

And if a user enters both the information then it should filter by applying both the condition.

I hope you can help me on this as well.


-- Vinay

 

 

GhanshyamChoudhariGhanshyamChoudhari
I believe this code satisfy the above requirement. please feel free to tweak the code.

Thanks