+ Start a Discussion
Starz26Starz26 

Pagereference Method and Return Null

I use it all the time,

 

BUT WHY.....

 

Why declare a method to return type PageReference and then return NULL.....

 

Sorry for the newb question.........

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Both PageReference and void work so the example is still valid.

 

My guess is that using PageReference makes it explicit that we're referring to an action method.

All Answers

jwetzlerjwetzler

I'm not totally sure what your question is so forgive me if it's stuff you already know, but you return a PageReference when you want to redirect or forward to a different page. You return null if you want to stay on the same page, usually because you're doing some type of partial page update.

 

You can also make your action methods void so you don't have to return null.

Starz26Starz26

Ok, so in many of the examples SFDC put in their training, or examples you see on the net do something like this:

 

List<String> lStr = New List<String>();

 

public PageReference doSomething(){

 

   // random stuff

      lStr.add('String1');

      lStr.add('String2');

   //end random stuff

  

   return null;

 

 

}

 

Why do this versus just making the method void.......Why use that as an example.....One would be led to believe there is something purpose to it when it is used in many SFDC examples.

jwetzlerjwetzler

Both PageReference and void work so the example is still valid.

 

My guess is that using PageReference makes it explicit that we're referring to an action method.

This was selected as the best answer
Starz26Starz26

Thanks, that is what I figured but I want to make sure there was not some hidden meaning that needed to be decoded :)

 

 

ealexanderealexander
Sorry Saurabh but your answer has nothing to do with the question as to why use pagereference as the return value when all you will ever return is null. Good reference on returning a pagereference though