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
daniel.duartedaniel.duarte 

Get inputField Value in Controller Extension

Hello,

I am trying to develop a page that shows the records of a custom object in a panelGrid according to a filter, where the user specifies values for the object's fields. I want to write a controller extensions that fetches the records from the Database using SOQL, but I need to get the values from the page, after the user types it. It probably will be something like [select field1, field2 from customobject__c where field1 = :ApexPages.currentPage().... (i don't know the rest)]

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
gurumikegurumike

You need a property similar to this in your controller extension:

 

 

public String myFilter { get; set; }

 And you can query on it using SOQL as you might expect:

 

 

 

public void myAction()
{
MyObject__c[] stuff = [ select Id, Stuff__c from MyObject__c where Stuff__c = :myFilter ];
// etc.
}

In your VisualForce page, then you only need to do this to connect the input to your controller:

 

 

 

<apex:form>
...
<apex:inputText value="{!myFilter}" /> ...
<apex:commandButton value="Find Stuff" action="{!myAction}" />
...
</apex:form>

The apex:inputText element will update the value of myFilter in your controller automatically, whenever the form is submitted (i.e. any action is taken).  That's why VisualForce and Apex are great... it's easy to connect your UI to your data.

 

 

All Answers

gurumikegurumike

You need a property similar to this in your controller extension:

 

 

public String myFilter { get; set; }

 And you can query on it using SOQL as you might expect:

 

 

 

public void myAction()
{
MyObject__c[] stuff = [ select Id, Stuff__c from MyObject__c where Stuff__c = :myFilter ];
// etc.
}

In your VisualForce page, then you only need to do this to connect the input to your controller:

 

 

 

<apex:form>
...
<apex:inputText value="{!myFilter}" /> ...
<apex:commandButton value="Find Stuff" action="{!myAction}" />
...
</apex:form>

The apex:inputText element will update the value of myFilter in your controller automatically, whenever the form is submitted (i.e. any action is taken).  That's why VisualForce and Apex are great... it's easy to connect your UI to your data.

 

 

This was selected as the best answer
daniel.duartedaniel.duarte

Thanks a lot.

I also found a way of using inputField instead of inputText, because I have some picklist fields and relationship that I don't want the user to type a value, I want the user to select one of the options.

In the controller:

public MyObject__c myObject {
   get {
      if (myObject == null)
         myObject = new MyObject__c();
      return myObject;
   }
   set;
}
myObjectList = [select name from MyObject__c where MyObject__c.Status__c = :myobject.Status__c];

 

In the page:

<apex:inputField value="{!myObject.Status__c}"/>