1

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); 

    }

}
2
  • what action on the page do you want to trigger the search? A button? An input being changed? Clearly, when the page is loaded you won't have any search criteria filled in. – Guy Clairbois Jul 2 '14 at 15:53
  • Since there are two fields, a button click is what I expect even though I'm trying to keep keyclicks to a minimum. – Mike Jul 2 '14 at 16:02
1

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;
}
6
  • Okay. @Jagular, this is part of what I don't understand. If I use InputField, I need to use the standardController. I also must use a reference to the object's fields...which is why I have value="{!flight__c.name}". If I change that to "{!customObject.name}", I get Error: Unknown property 'Flight__cStandardController.customObject'. Since I'm defining this in an extension, why doesn't it look there? See why it's taking me so long? :) – Mike Jul 2 '14 at 16:09
  • Sorry, I missed that this was a controller extension. I've edited to expand my answer. I agree that getting VF/Apex working initially can be frustrating. I almost always start from a working example. – Jagular Jul 2 '14 at 16:27
  • Thanks for the input @Jagular. I updated my code snippets. Currently, the VF page shows me what I typed (line 11--victory!!) but the extension is not translating the variable reference (boo!). I'm guessing at the solution, throwing in code from other working controllers in response to errors, but nothing works yet (because I'm guessing). If I stumble upon the right combination, I'll update my code again. – Mike Jul 2 '14 at 20:54
  • I missed the need for a function behind the button. Sorry. I've no time now, but I'll take another look in a few hours. – Jagular Jul 2 '14 at 21:12
  • No hurry since I'm a step closer. The variable wasn't being translated because I had it (without the double quotes) as "WHERE Name = :thisFlight" instead of breaking it up and using escape sequences. Sigh. The query is now correct, returns 1 row (as expected), so now I have to get the pageBlockTable to use it. – Mike Jul 2 '14 at 21:21

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.