-1

How can I use the variable value from apex class into a visualforce page which has a standard controller?

Any help is appreciated.

3
  • Use the class as an extension. <apex:page extension="your_class"></apex:page> – highfive Sep 18 '15 at 6:06
  • Which class I have to use as extension? The class from where I want to access variable value ? – Khawaja M. Awais Sep 18 '15 at 6:23
  • Possible duplicate of access controller variable in javascript – glls Jun 28 '17 at 13:16
1

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.

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

5
  • If name is changed in a method other than Constructor, can we use it in same way as you did> – Khawaja M. Awais Sep 18 '15 at 6:50
  • Yes, but the method needs to be invoked by the constructor or a button click. – codeinprogress Sep 18 '15 at 6:55
  • This is not working for me. :( Its is showing me error, "Unknown constructor 'QuandlStockDetailsCntl.QuandlStockDetailsCntl(ApexPages.StandardController controller)' – Khawaja M. Awais Sep 18 '15 at 7:19
  • You constructor name is incorrect, it should be QuandlStockDetailsCntl(ApexPages.StandardController controller), assuming QuandlStockDetailsCntl is the name of the extension class. – codeinprogress Sep 18 '15 at 9:32
  • Yes QuandlStockDetailsCntl is name of extension class but I only include this and got error. – Khawaja M. Awais Sep 18 '15 at 12:23
0

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.

1
  • No, I just want to get value from an apex class in a visualforce page. Visualforce page has standard controller but I need value from a different apex class. Its something like hierarchy, 1- I have a visualforce pahe with standard controller, from where I click on a button, which opens a popup window that shows me a vf page with a custom controller. here from this page, I am making a page-reference to another page which also has a custom controller. Now this custom class has the variable that I need in 1st vf page. I think my question/scenario is clear to you? – Khawaja M. Awais Sep 18 '15 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.