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
Dustin MayfieldDustin Mayfield 

Pass variable from one controller to another VF page and controller

I use a controller that faciliates a type of "login" that queries a SF contact username and password fields and allows users to login to a dashboard where ideally they can navigate, by buttons, to various visualforce pages depending on their need. The main thing I'm stuck on is figuring out how to pass (ideally securely) their contact username to the various other controllers and VF pages. As an example, a user would login with username and password, it would take them to a dashboard where they can click on a button "PTO Request" and then it redirects to a new VF (with a new controller) and it will display at the top "Welcome Bob Smith". 
 
public without sharing class STGeneralLogin {
      
    public String STUsername {get;set;}
    public String STPassword {get;set;}
    public String result {get;set;}
    public String curPage{get;set;}
    public String contactID {get;set;}
    public List<Contact> c{get;set;}


   
    public PageReference doLogin(){
    
        c = [Select id,TKP_Password__c,Name from Contact where TKP_Username__c =: STUsername];
        if (c.size() > 0){
            if (STPassword.equals(c[0].TKP_Password__c)){
                contactID = c[0].id;
                result = 'True';
                
               
                PageReference pageRef = Page.STEmployeeApps;
                pageRef.setRedirect(false);
                
                return pageRef;
                
            }       
            else{
                result = 'Login failed, please try again.';
                return null;
            }
        }
        
        
        else{
            result = 'Login failed, please try again.';
            return null;
        }
    }
   

    public PageReference checkContact(){
        PageReference pageRef = Page.STLoginPage;
        PageReference pageRefPass = Page.STEmployeeApps;
            pageRef.setRedirect(true);
        try{
            if(c== null){
            
            return pageRef;
            }
        }
        
       catch(Exception e){
            // errorEmail (e);
        
            return pageRef;
        }
        return null;
 
	}
  
    public PageReference gotoPTOReq(){
        // PageReference pageRef = Page.PTO_Request || new PageReference('apex/PTO_Request');
       // newPage.getParameters().put(VAR_NAME, VALUE);
	   // newPage.setRedirect(true);
        
        return pageRef;
 
	}
}

Page 1 (VF apps Dashboard)
 
<apex:page showHeader="false" controller="STGeneralLogin" action="{!checkContact}"> 
<apex:pageBlock title="Employee Apps">	
	<apex:form >
		<apex:commandButton value="PTO Request" action="{!gotoPTOReq}"/>
	</apex:form>    
</apex:pageBlock>

</apex:page>

Page 2 (PTO Request)
<apex:page controller="PTORequest">
     
    <apex:form >
        <apex:inputField style="display:none" required="true" value="{!c.Name}"/>
        <apex:pageMessages />
        <apex:pageBlock title="Time Off Request">
            <apex:pageBlockSection >
            	<apex:inputField required="true" value="{!c.Type__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField required="true" value="{!c.Employee__c}"/>
            </apex:pageBlockSection>
             <apex:pageBlockSection >
                <apex:commandButton value="Submit Request" action="{!submitrequest}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

As you can see, the PTO Request page uses a different controller, which is what I think we would want... Are there any suggestions on how we may accomplish this? 
Gururaj BGururaj B
When you are using different controller it should have corresponding class. Either you use the same controller as first VF or create new class.
Dustin MayfieldDustin Mayfield
Thanks for your response, I'm not fully understanding though. We do have another class for the controller "PTORequest" I just didn't include it in the post as I didn't think it would be completely relavent.. 
 
public with sharing class PTORequest {
    public Time_Off_Request__c c { get; set; }

	public PTORequest() {
		c = new Time_Off_Request__c();
	
	}
	public PageReference submitrequest() {
		try {
			// now look for an associated contact with the same email
			User u = [SELECT Email FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
		
			// Specify DML options to ensure the assignment rules are executed
			Database.DMLOptions dmlOpts = new Database.DMLOptions();

			dmlOpts.assignmentRuleHeader.useDefaultRule = true;
			c.setOptions(dmlOpts);

			// Insert the case
			Database.Saveresult cResult = Database.insert(c);

			return new PageReference('/'+c.Id);
		} catch (Exception e) {
			ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'We could not find a user from your contact email. Please contact IT by emailing itsupport@saratechinc.com.'));
			return null;
		}
	}
}