Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Overriding a Page for Some, but not All, Users

Overriding a Page for Some, but not All, Users

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice

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:
      			<apex:page action=
      			  "{!if($Profile.Name !='System Administrator', 
      				null, 
      				urlFor($Action.Account.Tab, $ObjectType.Account,
      				null, true))}"
      			  standardController="Account" 
      			  recordSetVar="accounts" 
      			  tabStyle="Account">
      
      			  <!-- Replace with your markup -->  
      				
      			  This page replaces your Account home page for 
      			  all users except Administrators.
      			</apex:page>
      			
    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:
      			<apex:page standardController="account" extensions="overrideCon" action="{!redirect}">
      			  <apex:detail/>
      			</apex:page>
      			
      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:
    		<apex:page standardController="account">
    		  

    Override Account page for two profiles

    <apex:detail /> </apex:page>
    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:
      					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  
      						
      						  }
      					   }
      					}
      				
    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.

Share

Recipe Activity - Please Log in to write a comment

Is this code still valid? I am getting error "Insufficient privileges for accessing the page" 
 

by uma pen  (2014-11-20)

I tried the same code in my developer's edition, for one page i have given System Admin security setting and for another page i have used Marketing user. When i log in as marketing user I am getting error as Insufficient privileges for accessing the page.

Please let me know what was the issue.

by vivekanandan r  (2013-04-18)

Tested this recipe and worked flawlessly!

voted as verified by Jesus Garcia  (2012-10-18)

Great catch  Mans af Klercker!  I also noticed another circumstance where this would fail, even in an English org.  Salesforce allows you to create a custom profile with the same name of System Administrator.  Did you ever figure out a way to have it be Profile Name agnostic?

by Ramon Munoz  (2012-06-18)

I think this recipe should be modified to put the redirection logic outside of the content itself. This creates more flexibility for adding more custom pages later and separates the redirection logic into its own.

For example: PageDirectorController that contains the redirection logic, and a single simple page called PageDirector that calls the logic.

by a0930000008Oiok  (2011-02-22)

Now that this is fixed, it works fairly well, although it is a bit clumsy when you need to replace an edit page.  If you use the Action/URLFOR method, the page you are redirected to will not maintain the record variables in the edit page and the cancel button won't return you to the object list page (it just reloads the same page).

The controller extension method works much better, but you have to rebuild the standard page and when you need to make an Edit page, there is no single line equivalent to the <apex:detail /> for a form.

by egranger05182009  (2010-10-18)

I'm wondering if this recipe works if the user running the code has SF localized to another language? I have had test code that looks up a Profile from Name =  'System Administrator' that runs fine when it's run as a user with english language, but failed when runing as a user with different localization (swedish for example, where the name of this profile is 'Systemadministratör'). I would be very interested in how to look up a certain profile in a language-neutral way

by Mans af Klercker  (2010-10-13)

 egranger05182009, thanks for reporting this.  It looks like the recipe was incorrectly transferred to the system.  It's been repaired now.

by memestorm  (2010-10-11)

I'm having trouble with this recipie.  First of all, the VisualForce code is the same for all 3 pages which doesn't seem right.

Secondly, when creating the controller extension, I get a compile error: " line 1:30 no viable alternative at character '"' at line 1 column 30"  I think the posted code for the extension is wrong.  It should start with public class overrideCon{}....  The 5 lines posted there should be in the VF pages calling the controller.

by egranger05182009  (2010-10-08)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.