[SalesForce] How to accept InputField to filter a query and then display results

To save you a search, similar examples exist at how to pass value from visual force page (InputField) to controller? and https://stackoverflow.com/questions/10556002/salesforce-how-do-i-pass-value-of-apexinputfield-to-custom-extension. I'm using them as guides right now in hopes I'll suddenly develop a brain but, until then, I'll post/vent.

I want to search a custom object using two input fields from a VF page and display the result(s) in a pageBlockTable below the search section (or another page–I really don't care at this point). I'm a bit lost as to a working solution and think I need to do the following:

  • accept the input as strings (in an extension)
  • build the query
  • execute the query
  • populate a list element
  • display the list

Over the past two days, I've tried different combinations for each of the above bullets but keep getting blocked. The query runs with a null value, the query returns more rows than expected and the VF page can't display them, the return type is not the same as expected–all I get is stymied.

What adds to my frustration is knowing that this is the most basic of exercises. A step beyond "Hello world" is running a query to filter something. Egads, what I want cannot be that complicated–I just can't figure it out. BTW, this code will be replaced by an API call but, until then, this is just the first of three similar steps.

Thanks for letting me vent and, as always, thanks in advance.

Mike

VF page (which displays what I type in line 11–outputText):

<apex:page standardcontroller="Flight__c" extensions="FlightExtension">
    <apex:form >
        <apex:pageBlock title="Find Flights">
            <apex:pageMessages />
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!findThis}" value="Find flights"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputField value="{!Flight__c.name}" label="Flight #"/>
                <apex:inputField value="{!Flight__c.Journey_Date__c}" label="Date of your journey"/>
                <apex:outputText value="{!thisFlight}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!findFlights}" var="f">
                <apex:column >{!f.Name}</apex:column>
                <apex:column >{!f.TakeoffCity__c}</apex:column>
                <apex:column >{!f.TakeoffTime__c}</apex:column>
                <apex:column >{!f.LandingCity__c}</apex:column>
                <apex:column >{!f.LandingTime__c}</apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller extension (tweaked to have dynamic SOQL statement):

public with sharing class FlightExtension 
{

    public ApexPages.StandardSetController ssc {get; private set;}
    private Flight__c flt;
    public String thisFlight {get; set;}
    String query = 'Select Id, Name, TakeoffCity__c, TakeoffTime__c, LandingCity__c, LandingTime__c From Flight__c ';

    public FlightExtension(ApexPages.StandardController controller) 
    {
        flt = (Flight__c) controller.getRecord();
    }

    public PageReference findThis ()
    {
        thisFlight = flt.Name;
        ssc = new ApexPages.StandardSetController(Database.Query(query));
        if(ssc.GetRecords().Size() == 0)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'No matching flights found.'));
        }

        return null;
    }

    public List<Flight__c> getfindFlights() 
    {
        if(thisFlight != null)
        {
            query += ' Where Name = \'' + thisFlight + '\'';
        }
        return Database.query(query); 

    }

}

Best Answer

You need to make the connection between the Visualforce page and the Apex class. I do that like this:

VF Page

<apex:pageBlockSectionItem >
<apex:outputLabel value="Program" for="Program"></apex:outputLabel>
<apex:inputText id="Program" title="Program" value="{!Program}"/> 
</apex:pageBlockSectionItem> 

Apex code

public String Program {get; set;}

Your line:

private Flight__c customObject;

gives you a variable, "customObject" that I don't see referenced in the VF page.

edited

I don't see where you make the connection to the standard controller. Here's a trimmed example:

public class TheControllerExt {

private ApexPages.StandardController controller;

public TheControllerExt(ApexPages.StandardController controller) 
{
this.controller = controller;
}
Related Topic