+ Start a Discussion
Sivakumari2iSivakumari2i 

Passing value from controller to its extension

Hi,

 

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) 
hitesh90hitesh90

Hi,

 

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

 

Visualfoorce Page:

 

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

 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() {
        system.debug('########'+strCountry);
        PageReference pr = new PageReference('http://www.google.com');
        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(); 
        system.debug('########'+strExtcountry);
    }
}

 

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.

Thanks,

Hitesh Patel