Salesforce | Passing value of the variables from Visualforce page to the controller

|
| By Webner

Problem : Accessing value of the variables on Visualforce page within the Controller.

Solution: We can access the value of the variables in the Controller of the Visualforce page by declaring the variables in Controller as public and get, set types like public type var1 {get;set;}

Example:

public class exampleContoller
{
public String textId {get;set;}
public String textName {get;set;}
public exampleContoller()
{
}
public PageReference Submit()
{
 //body of submit function
}
}

Access these variables on Visualforce page as:

<apex:page controller=”exampleContoller”>
<apex:form>
<apex:inputtext value=”{!textId }”/>
<apex:inputtext value=”{!textName}”/>
<apex:commandbutton action=”{!submit}” value=”save”/>
</apex:form>
</apex:page>

When we load this page, enter values in input textboxes and submit page then these values are directly accessible by controller and need not to pass these variables on submit.

We can access these variables as following:

public class exampleContoller
{
public String textId {get;set;}
public String textName {get;set;}
public exampleContoller()
{
}
public PageReference Submit()
{
system.debug('textId =='+textId);
system.debug('textName  =='+textName);
//here we do what we want to do with these variables
}
}

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at salesforce@webners.com

Leave a Reply

Your email address will not be published. Required fields are marked *