Overriding a Page for Some, but not All, Users

Source code:

Problem

Some of your users should use a custom Visualforce page, while others should use a standard Salesforce.com page.

Solution

To override the Account tab with a Visualforce page for most of your users, but send users with the “System Administrator” profile to the standard Salesforce.com Account home page:
  1. Create a Visualforce page called *conditionalAccountOverride*.
    1. Click Setup | Develop | Pages.
    2. Click New.
    3. In the label field, enter Override the Account Page for Most Users.
    4. In the name field, enter accountOverride.
    5. In the description, enter This page will display for all users, except System Administrators, when they click the Account tab.
    6. In the editor, enter the following markup: [code visualforce] This page replaces your Account home page for all users except Administrators. [/code]
    7. Click Save.
  2. Set the page level security to allow all users to view the page.
    1. Click Security for the page you just created.
    2. Select all the profiles that will be using this page in the Available Profiles list and click Add to add them to the Enabled Profiles list.
    3. Click Save.
  3. Create an override that directs users to your new page.
    1. Click Setup | Customize | Accounts | Buttons and Links.
    2. In the Standard Buttons and Links list, click Override for the Accounts Tab.
    3. Set the content type to Visualforce Page.
    4. From the Content Name drop-down list, select conditionalAccountOverride.
    5. Click Save.

Discussion

This solution uses the *action* attribute on the *apex:page* component to test the user's profile. Using the *action* attribute is a good way to ensure an action is taken when the page loads. If instead of limiting the standard page to a particular group of users, you want to limit the override to a particular group of users, such as all “Marketing Users” and “Solution Managers,” you need to create a controller extension. To override the Account tab with a custom Visualforce page only for users with the “Marketing User” or the “Solution Manager” profile:
  1. Create a Visualforce page called *standardAcctPage*.
    1. Click Setup | Develop | Pages.
    2. Click New.
    3. In the label field, enter Override the Account Page for Most Users.
    4. In the name field, enter standardAcctPage.
    5. In the description, enter This page will display for all users, except Marketing Users and Solution Managers, when they click the Account tab.
    6. In the editor, enter the following markup: [code visualforce] [/code] This page overrides your current Account home page. It uses a controller extension to test the user profile. If the user is a “Marketing User” or “Solution Manager,” they are redirected to a different page.
  2. Using the procedure in step 1, create a second Visualforce page called *customAcctPage*: [code visualforce]

    Override Account page for two profiles

    [/code] This is the page that only the “Marketing Users” and “Solutions Managers” will see. They only get to this page through redirection.
  3. Grant access to both pages for all profiles.
    1. Click Security for the each of the pages you just created.
    2. Select all the profiles that will be using this page in the Available Profiles list and click Add to add them to the Enabled Profiles list.
    3. Click Save.
  4. Create a controller extension called overrideCon.
    1. Click Setup | Develop | Apex Classes.
    2. Click New.
    3. In the editor, add the following content: [code apex] public class overrideCon { String recordId; public overrideCon(ApexPages.StandardController controller) {recordId = controller.getId();} public PageReference redirect() { Profile p = [select name from Profile where id = :UserInfo.getProfileId()]; if ('Marketing User'.equals(p.name) || 'Solution Manager'.equals(p.name)) { PageReference customPage = Page.customAccountPage; customPage.setRedirect(true); customPage.getParameters().put('id', recordId); return customPage; } else { return null; //otherwise stay on the same page } } } [/code]
    4. Click Save.
  5. Create an override that directs users to the standardAcctPage page when they click on the Accounts tab.
    1. Click Setup | Customize | Accounts | Buttons and Links.
    2. In the Standard Buttons and Links list, click Override for the Accounts Tab.
    3. Set the content type to Visualforce Page.
    4. From the Content Name drop-down list, select standardAcctPage.
    5. Click Save.
    When a user clicks on the Account tab, if their profile is “Marketing User” or “Solution Manager” the controller extension will automatically redirect them to the customAcctPage.