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

Passing value from controller to its extension



I have a custom controller ( with sharing class is used)  and a visualforce page. I have some business logic in without sharing class.(i.e) I need some logic in with sharing class and some logic in without sharing class and both results are returned to the same visualforce page.

How to achieve this?


I tried with controller extension. But i face following problem
(i.e) i have a picklist in visualforce page and id is returned to with sharing class(custom controller). But i don know how to get that selected picklist id in the controller extension(without sharing class).


I also tried in creating inner class as without sharing inside outer class as with sharing. But i dont know how to access the methods in inner class and dont know how to return the value from inner class to visualforce page.


Please help me to resolve this issue.

Best Answer chosen by Admin (Salesforce Developers) 



here is the example for Passing value from controller to its extension.


Visualfoorce Page:


<apex:page controller="ctrlPassToExtention" extensions="extPassToExtension">
        <apex:selectList value="{!strCountry}"  size="1">
            <apex:selectOptions value="{!items}"/>
        <apex:commandButton value="Test" action="{!Redirectpage}" />

 Apex class: custom controller ( with sharing class is used) 


public with sharing class ctrlPassToExtention {
    public string strCountry {get; set;}
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    public PageReference Redirectpage() {
        PageReference pr = new PageReference('');
        return null;
    public String getCountry() {
        return strCountry;
    public void setCountry(String strCountry) {
        this.strCountry = strCountry;

 Apex class: controller extension(without sharing class).


public without sharing class extPassToExtension {
    public String strExtcountry {get; set;}
    public extPassToExtension(ctrlPassToExtention controller) {       
        strExtcountry  = (string) controller.getCountry(); 


Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.


Hitesh Patel