[SalesForce] Passing a variable into startUrl to be used on next page

I am working on trying to pass a variable from one VFP to another. Currently, I'm doing the Site.login(username, password, startUrl), and I technically can pass in a variable through the URL, however I don't want to do it that way.

Here is what I've got currently:

    PageReference loginResult = null;
    String startUrl = '/LoginRedirect?un='+username;
    loginResult = Site.login(username, password, startUrl);
    if (loginResult == null){
        this.result = 'Please check your username and password.';
    }
return loginResult;

Any ideas on the best practice way to pass a variable from this page to the next? If it makes a difference, both pages are using the same Controller.


EDIT:

Trying to do this and when I hit the 'login' button I'm getting redirected to an error page.

    PageReference loginResult = null;
    loginResult.getParameters().put('id', username);
    String startUrl = '/LoginRedirect';
    loginResult = Site.login(username, password, startUrl);

    if (loginResult == null){
        this.result = 'Please check your username and password.';
    }

return loginResult;
}

The actual error from logs is as follows

21:41:47.0 (19743152)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object — on the 'loginResult.getParameters().put('id',username);

Best Answer

Looking at the error from the logs ,looks like the user is not instantiating the pagereference object here

Below can be resolution for same

 PageReference loginResult = new PageReference();
  String startUrl = '/LoginRedirect?un='+username;
  loginResult = Site.login(username, password, startUrl);
  if (loginResult == null){
    this.result = 'Please check your username and password.';
 }
return loginResult;
Related Topic