Change a Tab to Open to a Default View

view-recent-itemsBy default, when you click on any standard Salesforce.com Tab for an Object, it brings you to a list of “Recently Viewed” items. I, like many others, find this annoying. I’d much rather that SalesForce.com give us options to change the default View by Tab, ideally for each user Profile.

In the mean time, the work-around was to use an S-Control to redirect the user to the custom view. However, with the upcoming deprecation of S-Controls in 2010, we should be writing these as VisualForce pages instead.

S-Control Version:

<html>
<head>
<script language="Javascript">
function init()
{
     parent.document.location.href= "/003?fcf=00B30000005a5FO";
}
</script>
</head>
<body onload="init()">
<center><h2>Please Wait ..... Loading Contacts Tab</h2></center>
</body>
</html>

VisualForce Version

So, I decided to take a stab at converting a more sophisticated version of the above S-Control into a VisualForce page. The key, though, was to completely avoid using Apex so that the page can be modified in Production without having to go through the Sandbox/Test/Deploy steps required with Apex. This is accomplished by using JavaScript for the page logic, basically the same as the S-Control. It just has to be wrapped within the Apex:Page tags and the startup JavaScript logic is slightly different.

The resulting VisualForce page shown below does the following:

  • Display a loading message.
  • Retrieves the Prefix for the Contact object (‘003’ typically). You can substitute Contact for any object that you want this page to work with.
  • Uses a simple Switch/Case statement based on the Users Role Name to determine which View to load. This is where it’s necessary hardcode the View ID’s. Of course, since we have no Apex it’s easy to modify these at any time on the fly.
  • Redirect the user to that view URL
<apex:Page tabStyle="Contact" >
<script src="/soap/ajax/15.0/connection.js"></script>
<script type="text/javascript" />
<script>
     window.onload = function() {

     sforce.connection.sessionId = '{!$Api.Session_ID}';

     var describeSObjectResult = sforce.connection.describeSObject("contact");
     var prefix = describeSObjectResult.keyPrefix;

     // Determine the View based on the Role of the User
     var cView;
     switch ( "{!$UserRole.Name}" ) {
     case "North America": cView = "00B30000001Ysw2" ; break ;
     case "EMEA": cView = "00B30000001Ysw4" ; break ;
     case "Europe": cView = "00B30000001Ysw5" ; break ;
     case "SEA": cView = "00B30000001Ysw6" ; break ;
     case "South East Asia": cView = "00B30000001Ysw7" ; break ;
     case "Australia": cView = "00B30000001Ysw8" ; break ;
     default: cView = "00B30000001Ysw3"; break;
     }

     // Change the whole window to point to this location
     parent.document.location.href = "/" + prefix + "?fcf=" + cView ;
}
</script>
<center><h2>Please Wait ..... Loading 'Your' Tab</h2></center>
</apex:page>

To implement this in your Org:

  • Copy the ID’s for each of the View’s you want reference in your page. If you want everyone to default to the same view, then you only need the one ID.
  • Create a new VisualForce Page and paste in the code above.
  • Replace “Contact” on the the tabStyle and describeSObject lines to the object you are creating a tab for
  • Modify the Role Names or use {!$Profile.Name} if you prefer to set the default view by Profile.
  • Modify the view ID’s in the Switch/Case lines as appropriate.
  • If you want everyone to go to the same view, delete the Switch/Case block and add = “view Id to the var cView; line.
  • Save your Page.
  • Override the Tab to point to this new VisualForce page
    override-tab