+ Start a Discussion
Pooja GLPooja GL 

Pass value selected on VF page on to an Apex Class that is not its Controller

Hi All,

I have a requirement where I have to use the value selected on the VF page in a different class other than its controller.
Is this do-able?

Please let me know if anyone has any information regarding this.

Thanks,
Pooja
SandhyaSandhya (Salesforce Developers) 
Hi Pooja,

In this scenario, you can make that class as an extension for you Visualforce Page.

You can have as many extensions as you need.

You can use extensions attribute of <apex:page> component for referring to any extensions you need. Refer below simple example from the documentation and understand how to bind the extension with the page and the constructor of the 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 + ')';
    }
}
 
<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>

Please refer below link for more information on extensions.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm



Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya