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
Mitchell CarlsonMitchell Carlson 

Clearing Search Results

Hi, 

I am new to apex and visual force, but have picked up somethings from helpful posters online. I am needing some help in clearing search results. I have written the Visual Force Page code and the Apex Class controller code to contain a "Clear" button, but when clicked the clear button only clears the text input field on the search and not the search results grid. Any thoughts on how to expand the Apex Class to also clear the search results?

Visualforce Markup:
<apex:page controller="search" >  
    <apex:form >
        <apex:outputLabel style="font-weight:bold;" value="Search By Client (Full Name or Email):" >
        </apex:outputLabel>
        &nbsp;<apex:inputText value="{!textData}"/>
           &nbsp;<apex:commandButton value="Search" action="{!result}"/> 
             &nbsp;<apex:commandButton value="Clear" action="{!clear}"/> 
           <br>
           </br>
        <apex:pageBlock >
              <apex:pageBlockTable value="{!opportunity}" var="opp">
                   <apex:column >
                        <apex:facet name="header">Full Name</apex:facet>
                        {!opp.Full_Name__c}
                   </apex:column>
                   <apex:column >
                        <apex:facet name="header">Technician</apex:facet>
                        {!opp.Technician__c}
                   </apex:column>
                  <apex:column >
                        <apex:facet name="header">Opportunity Year</apex:facet>
                        {!opp.Opportunity_Year__c}
                  </apex:column>
                  <apex:column >
                        <apex:facet name="header">Stage</apex:facet>
                        {!opp.StageName}
                  </apex:column>
                  <apex:column >
                        <apex:facet name="header">Status</apex:facet>
                        {!opp.Status__c}
                  </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>    
</apex:page>

Apex Class:
public class search 
{

    public PageReference clear() {
        textData = '';
        return null;
    }

    public string textData{get;set;}
    public List<Opportunity>Opportunity{get;set;}   
    public PageReference result()
    {
        Opportunity= [Select Full_Name__c, Technician__c, Opportunity_Year__c, StageName, Status__c from Opportunity where Full_Name__c Like :'%'+textData+'%' OR Client_Email__c Like :'%'+textData+'%'];    
        return null; 
    }
}

Thanks for your consideration!

Best Answer chosen by Mitchell Carlson
Suraj TripathiSuraj Tripathi

Hi Mitchell, 
Below is the Code for your requirement. Its working fine in my Org. Hopes it helps you.

Just add one more line to your clear Method :

Opportunity.clear();
so your Apex Class will be 

public class search 
{

    public PageReference clear() {
        textData = '';
        Opportunity.clear();
    }

    public string textData{get;set;}
    public List<Opportunity>Opportunity{get;set;}   
    public PageReference result()
    {
        Opportunity= [Select Full_Name__c, Technician__c, Opportunity_Year__c, StageName, Status__c from Opportunity where Full_Name__c Like :'%'+textData+'%' OR Client_Email__c Like :'%'+textData+'%'];    
        return null; 
    }
}

 

Please mark this answer best if it resolves your query.

Regard,
Suraj​​

All Answers

Suraj TripathiSuraj Tripathi

Hi Mitchell, 
Below is the Code for your requirement. Its working fine in my Org. Hopes it helps you.

Just add one more line to your clear Method :

Opportunity.clear();
so your Apex Class will be 

public class search 
{

    public PageReference clear() {
        textData = '';
        Opportunity.clear();
    }

    public string textData{get;set;}
    public List<Opportunity>Opportunity{get;set;}   
    public PageReference result()
    {
        Opportunity= [Select Full_Name__c, Technician__c, Opportunity_Year__c, StageName, Status__c from Opportunity where Full_Name__c Like :'%'+textData+'%' OR Client_Email__c Like :'%'+textData+'%'];    
        return null; 
    }
}

 

Please mark this answer best if it resolves your query.

Regard,
Suraj​​

This was selected as the best answer
Mitchell CarlsonMitchell Carlson
Suraj,
Thank you very much. You were extremely helpful. I think you overwrote "return null;" with the "Opportunity.clear();" line. The following is the apex class I used and it is working as the requirements dictate:

public class search 
{

    public PageReference clear() {
        textData = '';
        Opportunity.clear();
        return null;
    }

    public string textData{get;set;}
    public List<Opportunity>Opportunity{get;set;}   
    public PageReference result()
    {
        Opportunity= [Select Full_Name__c, Technician__c, Opportunity_Year__c, StageName, Status__c from Opportunity where Full_Name__c Like :'%'+textData+'%' OR Client_Email__c Like :'%'+textData+'%'];    
        return null; 
    }
}
Suraj TripathiSuraj Tripathi
Thank you, Mitchell, for selecting my answer as best. It's my pleasure to help you.
DoondiDoondi
If we don't enter any name or mail, just click on SEARCH, then it will display the entire list of opp's, (wont this confuse the User?)
Mitchell CarlsonMitchell Carlson
Doondi - Good point. Suraj, any thougths on how we can prohibit a wildcard search? Also how to display a "No Results Found" message if an improper search is performed? I appreciate any thought leadership on this.