How can I use the variable value from apex class into a visualforce page which has a standard controller?
Any help is appreciated.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityHow can I use the variable value from apex class into a visualforce page which has a standard controller?
Any help is appreciated.
You can use extensions
attribute of <apex:page>
component for refer to any extensions you need. Refer below simple example from the documentation and understand how to bind the extension with the page and specially the constructor of the extension.
Extension :
public class myControllerExtension {
private final Account acct;
// The extension constructor initializes the private member
// variable acct by using the getRecord method from the standard
// controller.
public myControllerExtension(ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord();
}
public String getGreeting() {
return 'Hello ' + acct.name + ' (' + acct.id + ')';
}
}
Page :
<apex:page standardController="Account" extensions="myControllerExtension">
{!greeting} <p/>
<apex:form>
<apex:inputField value="{!account.name}"/> <p/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>
You can see the variable greeting
is coming from the method getGreeting()
(getter of greeting
in other words) in the controller. Likewise you can declare variables in your extension class and bind them in the page as you do with a controller.
<apex:page standardController="object__c" extensions="MyExtension">
...
<apex:outputText value="{!name}"/>
</apex:page>
In your MyExtension
public class MyExtension {
public String name {get; set;}
private ApexPages.StandardController stdController;
public MyExtension(ApexPages.StandardController stdController) {
this.stdController = stdController;
getMyName();
}
private void getMyName() {
name = 'Hello World';
}
}
Hope this helps.
Awais,
If u want to use the value from controller to page then in that case u can just define the variable as an getter and setter method and use Apexpages.standardcontroller Variable_name and use the instance of this to use the functionality of standard object. For further help u can go through link https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_methods.htm
in case if u want to use value from the page to the controller then u can use .
<------------Controller---------->
public with sharing class Details{
public list<Account>myList{get;set;}
public list<Contact>myList1{get;set;}
Apexpages.standardcontroller controller;
public Details(Apexpages.standardcontroller con){
controller = con;
myList=new list<Account>();
myList=[select id,Name,AccountNumber,Phone,Website from Account];
String d = ApexPages.currentPage().getParameters().get('id');
//myList1 = new list<contact>();
myList1= [select id,Name,MobilePhone,Email,Birthdate from Contact where accountId= :d];
}
Public PageReference mycontactdetails(){
/*if( ApexPages.currentPage().getParameters().get('id')!= null)
myList1= [select id,Name,MobilePhone,Email,Birthdate from Contact where accountId= :ApexPages.currentPage().getParameters().get('id')];
System.debug(myList1);
*/return null;
}
} <--------------Page------------>
<apex:Form >
<apex:pageBlock >
<apex:pageBlockTable value="{!myList}" var="v" >
<apex:column value="{!v.Name}"/>
<apex:column value="{!v.AccountNumber}"/>
<apex:column value="{!v.Phone}"/>
<apex:column value="{!v.Website}"/>
<apex:commandLink action="{!mycontactdetails}" reRender="contactdetails" value="{!v.Name}"/>
<apex:param name="id" value="{!v.id}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:Form>
<apex:pageBlockTable value="{!myList1}" var="c" id="myList1">
<apex:column value="{!c.Name}"/>
<apex:column value="{!c.MobilePhone}"/>
<apex:column value="{!c.Email}"/>
<apex:column value="{!c.Birthdate}"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
Thanks.
<apex:page extension="your_class"></apex:page>
– highfive Sep 18 '15 at 6:06