0

I have used different search buttons but i want the same functionality which can be done with the help of single search button

also i type any matching string with the record name then it should return the list i.e. if there is a record with name 'laptop' then if i enter the l or lap or laptop then it should return the record with name laptop

VF Page--

<apex:page controller="searchcontrol">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputText value="{!nameString}" label="Search By Name" />
                <apex:commandButton value="Search" action="{!SearchByName}" />
                <apex:inputText value="{!emailString}" label="Search By Seller Email" />
                <apex:commandButton value="Search" action="{!SearchByEmail}" />
                <apex:inputText value="{!priceString}" label="Search By Price" />
                <apex:commandButton value="Search" action="{!SearchByPrice}" />
                <apex:pageBlockTable value="{!Mlist}" var="mer">
                    <apex:column value="{!mer.name}" />
                    <apex:column value="{!mer.Merchandise_Price__c}" />
                    <apex:column value="{!mer.Merchandise_Qunatity__c}" />
                    <apex:column value="{!mer.Merchandise_Size__c}" />
                    <apex:column value="{!mer.Seller_Email__c}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller--

public class searchcontrol {
    public String nameString {get;set;}
    public String emailString {get;set;}
    public Integer priceString {get;set;}
    public list < Merchandise__c > Mlist {get;set;}
    public searchcontrol() {
        Mlist = new list < Merchandise__c > ();
    }
    public void SearchByName() {
        Mlist = new list < Merchandise__c > ();
        // String val='\'%' + String.escapeSingleQuotes(nameString.trim()) + '%\'';
        Mlist = [Select id, name, Merchandise_Price__c, Merchandise_Qunatity__c, Merchandise_Size__c, Seller_Email__c, Merchandise_Description__c from Merchandise__c where name =: nameString];
    }
    public void SearchByEmail() {
        Mlist = new list < Merchandise__c > ();
        Mlist = [Select id, name, Merchandise_Price__c, Merchandise_Qunatity__c, Merchandise_Size__c, Seller_Email__c, Merchandise_Description__c from Merchandise__c where Seller_Email__c =: emailString];
    }
    public void SearchByPrice() {
        Mlist = new list < Merchandise__c > ();
        Mlist = [Select id, name, Merchandise_Price__c, Merchandise_Qunatity__c, Merchandise_Size__c, Seller_Email__c, Merchandise_Description__c from Merchandise__c where Merchandise_Price__c =: priceString];
    }
}
1
1

This can be achieved by a function similar to below:

public void search(){
    Mlist = new list < Merchandise__c > ();
    if(nameString!=null && nameString.trim()!='') || (emailString!=null && emailString.trim()!='') || (priceString!=null && priceString.trim()!='')){
        String query='Select id, name, Merchandise_Price__c, Merchandise_Qunatity__c, Merchandise_Size__c, Seller_Email__c, Merchandise_Description__c from Merchandise__c where'
        if(nameString!=null && nameString.trim()!='')
           query+='name like \'%'+nameString+'%\' ';
        if((nameString!=null && nameString.trim()!='') && (emailString!=null && emailString.trim()!='') )
           query+=' and ';
        if(emailString!=null && emailString.trim()!='')
           query+='Seller_Email__c = \''+emailString+'\' ';
        if((emailString!=null && emailString.trim()!='') && (priceString!=null && priceString.trim()!='') )|| ((nameString!=null && nameString.trim()!='') && (priceString!=null && priceString.trim()!=''))
           query+=' and ';
        if(priceString!=null && priceString.trim()!='')
           query+='Merchandise_Price__c = '+priceString+' ';
        Mlist=(list < Merchandise__c >)Database.query(query);
        }
    }

where search would be a common function called from all buttons.

0

You can use the single search function below to perform a search by all inputted fields.

public void doSearch(){
    String query = 'Select id, name, Merchandise_Price__c, Merchandise_Qunatity__c, Merchandise_Size__c, Seller_Email__c, Merchandise_Description__c from Merchandise__c where FILTER_GROUP';

    String filter = '';

    emailString = String.escapeSingleQuotes(emailString);
    if(String.isBlank(emailString) == false){
        if(String.isBlank(filter)){
            filter += ' Seller_Email__c LIKE \'' + emailString + '%\'';
        }
    }

    nameString= String.escapeSingleQuotes(nameString);
    if(String.isBlank(nameString) == false){
        if(String.isBlank(filter)){
            filter += ' name  LIKE \'' + nameString + '%\'';
        }else{
            filter += ' AND name LIKE \'' + nameString + '%\'';
        }
    }

    if(priceString != null){
        if(String.isBlank(filter)){
            filter += ' Merchandise_Price__c = ' + priceString;
        }else{
            filter += ' AND Merchandise_Price__c = ' + priceString;
        }
    }

    if(String.isBlank(filter)){
        filter = 'Id != \'\'';
    }

    query = query.replace('FILTER_GROUP', filter);
    Mlist= (List<Merchandise__c>) Database.query(query)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.