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
Zuhair ShaikhZuhair Shaikh 

Want to make a simple form that takes in a number and then searches records in a custom object containing that number

Hello folks. I have a custom object containing 125 records, from Joe Smith1 to Joe Smith125 (The numbers are part of the last name). I want to make a visualforce page that has one textbox and one button and if you enter a number like "12" in the text box and hit the button, it should then list all the records where Fname+Lname(Custom fields in the custom object) contain "12". The list should also be in a table/grid. I'd also appreciate help with creating a custom controller for this page.

This seems like a simple problem but none of the visualforce workbooks I have gone through let me know how to accomplish something like this. Thanks in advance.
Best Answer chosen by Zuhair Shaikh
LBKLBK
Here is a sample filter functionality on Opportunity.

Apex
public class OppsList{
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public String filterText {get; set;}
    public ApexPages.StandardSetController setCon {
        get {
			String sSOQL = 'SELECT Name, CloseDate FROM Opportunity';
            if ((filterText != null) && (filterText != '')){
                sSOQL += ' WHERE Name Like \'%' + filterText + '%\'';
            }
			setCon = new ApexPages.StandardSetController(Database.getQueryLocator(sSOQL));
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}
VF Page
 
<apex:page controller="OppsList">
	<apex:form>
    <apex:pageBlock>
    <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:inputText value="{!filterText}" id="filterText"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem>
            <apex:commandButton  id="Filter" value="Filter"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
        </apex:form>
        <apex:pageBlock>
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Let me know if this helps you.

 

All Answers

LBKLBK
Here is a sample filter functionality on Opportunity.

Apex
public class OppsList{
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public String filterText {get; set;}
    public ApexPages.StandardSetController setCon {
        get {
			String sSOQL = 'SELECT Name, CloseDate FROM Opportunity';
            if ((filterText != null) && (filterText != '')){
                sSOQL += ' WHERE Name Like \'%' + filterText + '%\'';
            }
			setCon = new ApexPages.StandardSetController(Database.getQueryLocator(sSOQL));
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}
VF Page
 
<apex:page controller="OppsList">
	<apex:form>
    <apex:pageBlock>
    <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:inputText value="{!filterText}" id="filterText"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem>
            <apex:commandButton  id="Filter" value="Filter"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
        </apex:form>
        <apex:pageBlock>
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Let me know if this helps you.

 
This was selected as the best answer
Zuhair ShaikhZuhair Shaikh
Hey LBK, thanks for helping out again. I am a little confused with the controller class. From what I can gather, the method takes in the filterText input from the visualforce page, and then returns a set of records. However, on what basis does it search for the records? Will it check anywhere in each record for that filterText? And now that I think about it, will SOSL work better for something like this? I appreciate the help.
Zuhair ShaikhZuhair Shaikh
After going throught the sample code, I was able to make the form that I wanted. Thanks a lot.