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
Sumant HegdeSumant Hegde 

Pass parameter from one apex controller to another

Hi,

I am building a public site with multiple pages. I need to pass some parameters on each page so that I can use those parameters in my SOQL queries and display more information in next pages. 
I can do it by passing parameter via URL, but I don't think that is a secured way of doing it. 

Can somebody suggest me if there is any other way of passing some values from one controller to another without using URL?
Raj VakatiRaj Vakati
you can do it by using input hidden fields 
or use local cache ... 
Akshay_DhimanAkshay_Dhiman
Hi Sumant Hegde,

1. A Simple solution is, you can create only one controller for both pages. and declare a variable as public variable and get, set it.
and use this variable in another method of that class. Then you do not need to send parameter.

2. Solution:-

PAGE 1 - if you see below page, we have an input text with a value which is bind to the controller. We have a button on this page, clicking on which you will be navigated to the next page.

<apex:page controller="Controller1" sidebar="true" showHeader="true" >
    <apex:form >
        <apex:inputtext value="{!inputText1}"  />  
        <br />
        <apex:commandButton value="Send" action="{!GoToPage2}" reRender="result"/>
    </apex:form>
</apex:page>


CONTROLLER 1 - this controller has inputText1 declared with get,set. It contains the method that passes this text in url appended in 'att' (assuming 'page two' is the name of your second page)

public class Controller1{
    public String inputText1 {get;set;}
    public Controller1(){
    }
    public pageReference GoToPage2(){
        PageReference pf = new PageReference('/apex/pagetwo?att='+inputText1);
        return pf;
    }
}


PAGE 2 - has inputText2 which is again bound with controller and value is displayed in VF page.

<apex:page controller="Controller2" sidebar="true" showHeader="true" >
    <apex:form >
        <apex:inputtext value="{!inputText2}"  />  
        <br />
    </apex:form>
</apex:page>



CONTROLLER 2- if you observe this, all we are doing is getting the 'att' from URL in a constructor and assigning to inputText2

public class Controller2{
    public String inputText2 {get;set;}
    public Controller2(){
        inputText2 = apexpages.currentpage().getparameters().get('att');
    }
}
Thus you ll observe that inputText1 is passed from page1 to page2 and displayed there. This is pretty generic example that I can think of!

if you found this answer helpful then please mark it as best answer so it can help others.   

Thanks
Akshay
 
Sumant HegdeSumant Hegde
Hi Akshay,

Thanks for your solutions.
I am already doing the same thing that you explained in your Solution2. But I want to avoid passing parameter via URL.

I tried your Solution1, but the variable value is null when I redirect to next page but use the same controller.
Shubham NandwanaShubham Nandwana
Hi Sumant,
You can use controller extensions for sharing a variable and directly access a variable on the Visualforce page from another controller.
Declare a controller extensions as :
<apex:page showHeader="false" standardController="User"
    extensions="ProfileTabUserController" >

    <apex:outputPanel >
        <p>You are viewing the profile of {!user.name}, 
        whose UID is {!subjectID}.</p>
        <br/>
    </apex:outputPanel>

</apex:page>

Declare the variable to be used as transient or public as by default a variable is private in apex class.
Now you can directly use the variable as you use other variables. Like in above code subjectID is used.
This way you can use the same variable in many Visualforce pages by adding extensions.
For reference, you can visit below links.
1. https://developer.salesforce.com/docs/atlas.en-us.salesforce_profile_tabs_cheatsheet.meta/salesforce_profile_tabs_cheatsheet/salesforce_profile_sample_visualforce_page.htm
2. https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

Hope it helps. Reply if any doubt is there.
Select this as best answer if it resolves your query.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
Akshay_DhimanAkshay_Dhiman

Hi Sumant Hegde,

Using Apex Cookies you can able to get this done 


reference: https://webkul.com/blog/using-cookie-class-apex/

If you found this answer helpful then please mark it as best answer so it can help others.

Thank You 
Akshay