function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ThathulaThathula 

Apex page action - redirect or not redirect to the Insufficient Privileges page after checking a condition

Hi all,

I've a VF page and its controller, 
I've used page action as below 
<apex:page action="{!validateUser}" 


in my controller i've a method like this

public pageReference validateUser(){

if(someconditionTrue)

        PageReference errorPage = new PageReference('apex/ErrorPage');
        errorPage.setRedirect(true);
        return errorPage;


}

else{


//I need to stay in this page.


}


I'm not sure sure how to code for stay in this page, because this page is always recursively called. 
Best Answer chosen by Thathula
shiva pendemshiva pendem
HI Thathula,

Since you want to redirect to current page so no need to return anything , IF you give return null, the page will be in the same page, Hope this what you want .

Thanks,
Shiva

 

All Answers

shiva pendemshiva pendem
HI Thathula

Find the below code , That might helps you.

public pageReference validateUser(){

if(True){

        PageReference errorPage = new PageReference('/apex/ErrorPage');
        errorPage.setRedirect(true);
        return errorPage;
}
else{
return null;
//I need to stay in this page.
}
}

Thanks,
Shiva
ThathulaThathula
Hi Shiva, Thank you for your reply.  You have return null, but return null doesn't redirect to the current page, and i need to avoid from recusively runnig the code.
Any idea?
 
shiva pendemshiva pendem
HI Thathula,

Since you want to redirect to current page so no need to return anything , IF you give return null, the page will be in the same page, Hope this what you want .

Thanks,
Shiva

 
This was selected as the best answer
ThathulaThathula
Thanks a lot Shiva, Actually it works, Thank you very much. Already marked as best answer, I've another question though, since i've so many similar pages, can i use a common class for validateUser() function. 
Can i call like <apex:page action="{!commonClass.validateUser}" 
shiva pendemshiva pendem
Yes, you can use if same logic you want to use in the multiple pages, Add that class as Extensions or controller in VFpgae