Andrejs Igumenovs February 2016

How to get an input value on a Visualforce page from an Apex controller?

Let's pretend that I have an sObject called MyCutomObject with the fields Column1, Column2, Column3, PickMeColumn and others. The type of the PickMeColumn is Picklist.

While it's easy to access the object's instance data in my page, I'm a bit stucked with how to get the user's input data from that page to be accessible inside the controller.

Page code:

<apex:page sidebar="false" standardController="MyCustomObject__c" extensions="MyCustomSearchController">
    <apex:form >
        <apex:pageBlock title="Search Criteria">
            <apex:pageBlockSection>
                <apex:inputField value="{!myObject.PickMeColumn__c}" />
            </apex:pageBlockSection>

            <apex:commandButton value="Search" id="SearchButton" action="{!search}"/>
        </apex:pageBlock>

        <apex:pageBlock title="Search Results">
            <apex:pageBlockTable value="{!myObjectList}" var="myObject">
                <apex:repeat value="{!myObject}" var="aRecord">
                    <apex:column value="{!aRecord.Column1__c}"/>
                    <apex:column value="{!aRecord.Column2__c}"/>
                    <apex:column value="{!aRecord.Column3__c}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
</apex:page>

Controller code:

public class MyCustomSearchController {
    public MyCutomObject__c myObject {get;set;}
    public List<MyCutomObject__c> myObjectList {get;set;}

    public MyCustomSearchController(ApexPages.StandardController controller) {
    }

    public PageReference search() {
        String ValueSelectedByUser = '??? Help!';
        myObjectList = [SELECT Column1__c, Column2__c, Column3__c FROM MyCutomObject__c WHERE PickMeColumn__c = Val        

Answers


Andrejs Igumenovs February 2016

In controller's code simply like so:

public class MyCustomSearchController {
    public MyCutomObject__c myObject {get;set;}
    public List<MyCutomObject__c> myObjectList {get;set;}

    public MyCustomSearchController(ApexPages.StandardController controller) {
        myObject = new myCustomObject__c(); // Must create the object!
    }

    public PageReference search() {
        String ValueSelectedByUser = myObject.PickMeColumn__c;
        myObjectList = [SELECT Column1__c, Column2__c, Column3__c FROM MyCutomObject__c WHERE PickMeColumn__c = :ValueSelectedByUser];
        // Have to add the colon
        return ApexPages.currentPage();
    }
}

Post Status

Asked in February 2016
Viewed 2,308 times
Voted 9
Answered 1 times

Search




Leave an answer