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
DrawloopSupportDrawloopSupport 

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?
Best Answer chosen by Admin (Salesforce Developers) 
iceberg4uiceberg4u

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

AlsoDougAlsoDoug

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

 

DrawloopSupportDrawloopSupport

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?

Sandip458Sandip458

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 : 

public class SkillsController {
private final ApexPages.StandardController controller;
private Skill__c cntr;



public SkillsController(ApexPages.StandardController controller) {

this.controller = controller;
cntr= (Skill__c )controller.getRecord();
}



public PageReference Save()
{
Skill__c newSkill = new Skill__c();
newSkill.FieldName__c = cntr.FieldName__c;

insert newSkill;
return null;
}
}


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..

 

 

 

iceberg4uiceberg4u

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

 

}

This was selected as the best answer
AlsoDougAlsoDoug

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.

 

 

DrawloopSupportDrawloopSupport
Thanks iceberg! That works perfectly. I've googled and googled to no avail. Hopefully this will help others as well.
gv007gv007
Some body tell me in value fields what are thing u trying to get believe some data so how much u want to get and after that where are u storing in org?.