You need to sign in to do that
Don't have an account?
DrawloopSupport
How to access a visualforce object / element from apex
Assuming the visualforce object (or element) is assigned an id (e.g. <apex:inputField id="someid" value="{!someval}" />), how would one go about referencing that object (inputField value) from apex?
Suppose,In your VF Page there is
<apex:page controller="<XController>">
<apex:form>
<apex:inputText value="{!ValueFromController}" id="TestValue"/>
</apex:form>
</apex:page>
You can then access the fields in your controller class
public with sharing class <XController>
{
public Strin testValue;
/* //C# Style
public String ValueFromController
{
get{return testValue;}
set{testValue = value;}
} */
/*//Java Style
public String getValueFromController()
{
return testValue;
}
public void setValueFromController(String value)
{
testValue = value;
}
*/
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
}
All Answers
Not the super expert on force yet but it should be as easy as having in your controller/apex code
public String someval{get;set;}
You shouldn't even need the id. Its the value that binds to the controller/apex code.
Doug
So I have two problems with the code you wrote.
1. I'm getting an error: "Compile Error: unexpected token: ;" Perhaps the syntax is different?
2. I don't have a value set in the visualforce element. If I try to set the value to {!someval} with the apex code
public string someval;
I get this error: Read only property 'classname.someval'
I have tried creating a get and set function for this string but I'm not sure on the syntax for them.
Basically, I have an empty field that the user fills in and on the click of a button, I need access to that value. Perhaps I need to submit the form?
If you want to reference that value then use extension u will direclty get that value
without writing setters and getters.
See the below example :
2. Way is write Setters And Getters for this field it will set the value..
3. use ApexPages.currentPage().getParameters().get('id') to retrive the value of that element in action method.
I hope this will work for you..
Suppose,In your VF Page there is
<apex:page controller="<XController>">
<apex:form>
<apex:inputText value="{!ValueFromController}" id="TestValue"/>
</apex:form>
</apex:page>
You can then access the fields in your controller class
public with sharing class <XController>
{
public Strin testValue;
/* //C# Style
public String ValueFromController
{
get{return testValue;}
set{testValue = value;}
} */
/*//Java Style
public String getValueFromController()
{
return testValue;
}
public void setValueFromController(String value)
{
testValue = value;
}
*/
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
}
iceberg4u
Sorry once again still new to this whole thing but I thought (assumed) that using
{get;set;}
was a "convenience" way to write the getters and setters without having to write them out like you did.
Drawloopsupport
Not sure exaclty what your trying to do but I have a visual force page where I need to get values from a form and use them to search. Here is more or less the code:
Visual Force Page
<apex:page controller="MemberSearchController">
<apex:form>
<apex:inputText value="{!searchName}" id="searchName" />
<apex:inputText value="{!searchPhone}" id="searchPhone" />
<apex:commandButton action="{!doSearch}" value="Search" />
</apex:form>
</apex:page>
Controller
global class MemberSearchController {
static string recordType = 'Member'
public String searchPhone{get;set;}
public String searchName{get;set;}
private List<Account> accounts = new List<Account>();
public List<Account> getAccounts() {
String queryString = 'SELECT id, name, lastmodifieddate, type, recordType.name, phone ';
queryString += 'FROM Account ';
queryString += 'WHERE recordType.name = \'' + recordType + '\'';
if( this.searchName != null && this.searchName.length() > 0){
queryString += ' AND name like \'%' + this.searchName + '%\'';
}
if( this.searchPhone != null && this.searchPhone.length() > 0){
queryString += ' AND phone like \'%' + this.searchPhone + '%\'';
}
queryString += ' LIMIT 20';
this.accounts = Database.query(queryString) ;
return this.accounts;
}
public PageReference doSearch() {
PageReference searchResultsPage = new PageReference('/apex/memberSearchResults');
searchResultsPage.setRedirect(false);
return searchResultsPage;
}
}
Not going to defend this as perfect code but it works.
I am just calling the values in the code above like any other variable and they are populated with the values from the form.