1

I'm trying to pass the value from a public page variable to another page (both using same controller) without using parameters.

public String variable {get;set;}

The point is on my first page the variable retrieves the value I want but on the second one I have this statement that keeps on doing:

if(String.isEmpty(variable)){
     variable = 'not passing';
}

So why is this happening and what do I need to do to achieve that variable keeps the same value that retrieves on first page?

EDIT

public class DemoClass(){
     public String variable {get;set;}
     public DemoClass(){
          variable = variable == null ? ApexPages.currentPage().getParameters().get('variable'):variable;
     }
     public PageReference methodCallSecondPage(){
          PageReference ref = new PageReference('/path/to/second/page');
          ref.setRedirect(false);
          return ref;
     }
}

2 Answers 2

3

The value you have saved in your controller is part of view state. That being said, when you redirect you want your view state to be preserved.

This can be done using setRedirect method. Just set the parameter as false.

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

public class RedirectDemo{
    public Id transferToUserId  {get; set;}
    public RedirectDemo(){
        transferToUserId  = Apexpages.currentpage().getparameters().get('id');
    }

    public Pagereference redirectPage2(){
        Pagereference pg = new Pagereference('/apex/Page2');
        pg.setRedirect(false);
        return pg;
    }
}

Source: https://salesforce.stackexchange.com/a/137729/19118

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_setRedirect.htm

5
  • I added the setRedirect(false) part to the method and still not retrieving the corresponding value. But thanks anyaway!
    – molinet
    Commented May 29, 2018 at 9:17
  • Can you post complete code? we would b able to debug it better Commented May 29, 2018 at 9:23
  • I can't cause I'm working with client but the issue is that I have a constructor that sets the variable if it's null (which on first page it's true) but on second one maintains its value variable = variable==null?ApexPages().currentPage().getParameters().get('variable'):variable and a PageReference method with setRedirect(false) that shows this value but when I check the debugging logs second page does not keep value given on the first one...
    – molinet
    Commented May 29, 2018 at 9:31
  • Can you replicate the same in a pseudo code? You dont have to post exact code. Minimum code to reproduce should be enough. Commented May 29, 2018 at 9:33
  • I've just edited the question with an example code.
    – molinet
    Commented May 29, 2018 at 9:39
0

You might take a complete different approach. Instead of creating 2 VisualForce pages, create 2 VisualForce components and put them into the same page with an expression that only shows one or the other. This way you stay in the same view context. If you need the second page available without going through the first (do you?) then you could either call the combined page with a parameter or reuse the component on an additional page.

It’s also a good preparation to think in components rather than pages - the Lightning way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .