Overriding a Standard button is pretty straightforward.. but when you override a standard button with a visualforce page all users in your organization would see this page...
So, let's suppose you want one set of users to view a page (for ex: viewpage1) and another set of users to view another page (for ex:viewpage2).
First of all, create the two visualforce pages viewpage1 and viewpage2
Secondly, create a new visualforce page called "viewpageredir". This page would contain the following code...
Note that i have created this page for the "Account" object. I would use this page to override my Account View button. You can use this code for any object by specifying the name of the object in the standardcontroller attribute.
The Apex Code would be...
Now, let's see how this works.. As you can see "viewpageredir" has only one tag "<apex:page>" . There is no other content in this page as we are using it only to redirect the user to his specific page.
We, have an action attribute in the page. The action method is called when the page loads. This causes the method "pageredirect" to be called. In this page we check the current user's Username. "Userinfo" can not only get the username but also the ID, ProfileId etc. See the Apex Language reference for the entire list.
Based on the username, we redirect the user to the desired page......
So, let's suppose you want one set of users to view a page (for ex: viewpage1) and another set of users to view another page (for ex:viewpage2).
First of all, create the two visualforce pages viewpage1 and viewpage2
Secondly, create a new visualforce page called "viewpageredir". This page would contain the following code...
Note that i have created this page for the "Account" object. I would use this page to override my Account View button. You can use this code for any object by specifying the name of the object in the standardcontroller attribute.
<apex:page showheader="false" action="{!pageredirect}" standardcontroller="Account" extensions="pageredir">
</apex:page>
The Apex Code would be...
public class pageredir
{
public pageredir(ApexPages.StandardController controller) {
}
public Pagereference pageredirect()
{
if (Userinfo.getUserName() == 'testview1@org.com')
{
Pagereference p = Page.viewpage1;
return p;
}
else
{
Pagereference p = Page.viewpage2;
return p;
}
}
}
Now, let's see how this works.. As you can see "viewpageredir" has only one tag "<apex:page>" . There is no other content in this page as we are using it only to redirect the user to his specific page.
We, have an action attribute in the page. The action method is called when the page loads. This causes the method "pageredirect" to be called. In this page we check the current user's Username. "Userinfo" can not only get the username but also the ID, ProfileId etc. See the Apex Language reference for the entire list.
Based on the username, we redirect the user to the desired page......