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
Ola BamideleOla Bamidele 

PageReference is not working

Hi people, 

I have this apex code that redirects to another Visualforce page when clicked. When I have 3 PageReferences in the apex, they all work.

However when I have 4 PageReference, they all dont work.

Does anyone know why this is by any chance please? 
 
public with sharing class CustomerSatisfactionFR2{
  public String currentRecordId {get;set;}


  public CustomerSatisfactionFR2(ApexPages.StandardController controller){
    this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
  }
  
    
  public PageReference UK_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_EN?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }
 
      public PageReference France_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_FR_2?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }


	  public PageReference Dutch_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_NL?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }    

    
    	  public PageReference Germany_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_DE?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }
    
    
    
        	  public PageReference Italy_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_IT?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }
    
   
 
}

Thanks you very much!  
Waqar Hussain SFWaqar Hussain SF
Strange issue. The code looks fine.
Briana L.Briana L.
I would recommend that rather than having so many different controller methods, you have one controller method that controls the page reference, combined with a public variable for the country code/page redirect and a helper class. So in your VF code where you would have called the page redirect, instead call the helper method with the correct page name parameter.

 
public with sharing class CustomerSatisfactionFR2{
  public String currentRecordId {get;set;}
  public String VFPageName{get;set;}

  public CustomerSatisfactionFR2(ApexPages.StandardController controller){
    this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
  }
  
    
  public PageReference RedirectHelper (String VFPageName) {                   
  		  this.VFPageName = VFPageName;
                  pageRedirect()
  }
 
      public PageReference pageRedirect() {                   
  		   PageReference pageref = new 
                     PageReference('/apex/' + VFPageName + '?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }


 
}



 
Ola BamideleOla Bamidele
Hi Waqar Hussain SF, 

As thats what I was thinking, so I am not sure why it is not working - so I was wondering if maybe their is a limit of pagereferences or methods that I could have in this code.


Thanks
Ola BamideleOla Bamidele
Hi Briana L, 

I im not 100% sure if I want to change my whole script - especially as im not 100% sure I will be able to adapt mine to the way you sugguested :D. 

Also do you think that me changing the code to the way you suggested will make all of the page reference wrk correctly?

Thanks 
Ola BamideleOla Bamidele
Hi Briana L, 

I understand a bit more now. I've added your code to my apex code however theres one error that I cant overcome. 

On line 10, I am getting this error:
Missing return statement required return type: System.PageReference
Do you know why this is? 

Thanks very much!
Waqar Hussain SFWaqar Hussain SF
public with sharing class CustomerSatisfactionFR2{
  public String currentRecordId {get;set;}
  public String VFPageName{get;set;}

  public CustomerSatisfactionFR2(ApexPages.StandardController controller){
    this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
  }
  
    
  public PageReference RedirectHelper (String VFPageName) {                   
  		  this.VFPageName = VFPageName;
                  pageRedirect();
                  return null;
  }
 
      public PageReference pageRedirect() {                   
  		   PageReference pageref = new 
                     PageReference('/apex/' + VFPageName + '?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }


 
}

 
Briana L.Briana L.
Hi Ola,

Sorry, the issue is that the "redirect helper" is declared as public PageReference, so it needs to return page reference. My code was more pseudo-code to show you the idea of what I meant, I did not check it for syntax errors such as this.

You can either change it to public void or as Waqar suggested add the return statement return null to the helper method.