2

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);

  • 1
    You can use pagereference method itself to pass the start URL . – Mohith Shrivastava Jan 31 '16 at 20:15
  • Made an edit of going with the pagereference – David Litton Jan 31 '16 at 20:38
  • 1
    CHeck your debug logs to figure whats the error ?In sites if error occurs page is redirected automatically to the error page configured . – Mohith Shrivastava Jan 31 '16 at 21:33
  • Getting: 21:41:47.0 (19743152)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object --- on the 'loginResult.getParameters().put('id',username); line – David Litton Jan 31 '16 at 21:43
  • 1
    This simply means your username parameter is null here . – Mohith Shrivastava Jan 31 '16 at 22:10
2

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;
  • Trying to pass the parameter without using it in the URL. Assigning the PageReference () piece worked, but as for passing a variable through the loginResult.getParameters().put('id', username); I'm coming up short. – David Litton Jan 31 '16 at 23:02
  • Actually I should be set... got it working. – David Litton Jan 31 '16 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.