[SalesForce] Can a page reference method be called from within a void method

My use case was–>
referenced a void method in the action attribute on an apex:page
In the void method, check for a condition.
if true, do nothing,
else call a pagereference method to redirect to another page.

I tried using a void method here since the general opinion seems to be that using a pagereference method and returning null is bad programming.

But this didnt work, the page never redirected when expected. The same logic with a pagereference returning null works fine.

Best Answer

IN your specific case where it could return a pagereference or not based on a condition, using a pagereference as a return type is the proper way to do it.

The only time having a signature that returns a pagereference is "bad programming" is when it ALWAYS returns null;

Related Topic