Change a Tab to Open to a Default View

mgsmith | Friday, June 5th, 2009 | 15 Comments »

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
Share

15 Comments

  1. Kristin says:

    Thank you for sharing this idea and the sample code for the VF page. Let me start by saying I’m quite familiar with Salesforce Administration but green when it comes to VF…

    I tried your solution and one thing I noticed is that I’m not able to keep the “focus” on my tab. Using standard behavior as an example — I click a tab and it’s highlighted (this highlighting of the tab is what I’m calling “focus”). The focus will stay on my tab as I view existing records or create a new one. I like how it does this.

    As it is now with the VF page/tab, however, I click on my tab but it does not get highlighted (and it stays non-highlighted as I click on records, etc). I tried changing the tabStyle in your code to point to my custom tab instead of the object…that works until the page finishes loading and then I’m back to where I was with no focus. Any ideas?

    Also, if I’m fine with pointing everyone to the same view (my real goal for overriding the page is to hide the New button), are there benefits to using a solution like yours with JavaScript, as compared to something more simple like this?

    The only thing I have noticed so far is that with my solution the list views do not show with enhanced list capabilities, even though that’s enabled for my org (whereas list views do show with enhanced list capabilities in your solution).

  2. Kristin says:

    Just noticed that my sample VF code did not come through in the posting. Please let me know if there’s a specific way to share it.

  3. mgsmith says:

    Kristen,

    Try removing the < and > symbols from your code or replacing them with their character entity equivelent (ex: < = & l t ; (remove the spaces))

    Best Regards,

    Mike

  4. Todd Kruse says:

    This example is incredible, thank you very much for showing it to all of us. I do have one question. Is there a way to grab the value you are assigning to the cView dynamically? The reason I ask is, when testing this in our staging environment it works great, but when I move this over to our production side, I have to go in and change the hardcoded values (00B30000001Ysw2, 00B30000001Ysw4, etc) to what our production environment would use. Again, the solution is awsome, but the ability to just code once and forget would be a great help.

    Thanks

  5. mgsmith says:

    Todd,

    I wish there was a way to get field ID’s dynamically. Unfortunately, this is still not yet supported by SalesForce.com.

    Best Regards,

    Mike

  6. Bidhan says:

    I couldn’t make this to work — for some reason the onLoad function is not getting called.So, I get “lease Wait ….. Loading ‘Your’ Tab — and it stays there. The URL is still:

    https://c.na7.visual.force.com/apex/TestPage?save_new=1&sfdc.override=1

    What am I doing wrong?

    Thanks in advance for your help

  7. Bidhan says:

    Sorry — my bad. I had a typo and that caused all the problems.
    Works perfectly!!

  8. jason says:

    can someone post with their code with all Case fields filled out.

    I’ve butchered the above code enough.

  9. Anil says:

    This was great post..

  10. Michael says:

    Hi,

    thanks for this great post, but I wonder if there is a better way to do it now. The solution is great but we lose the tab highlighting and also it is overhead to create a new VF page for a lot of tabs just to redirect them to the custom list view.

    Thanks in advance
    Michael

  11. mgsmith says:

    Michael,

    I think you’re probably correct on this. It’s been a couple of years since I wrote that, plus the original idea went back even further. The main problem is that overriding a tab requires a VF page, and I believe each tab would require it’s own VF page. The tab highlighting shouldn’t be affected though because of the tabStyle=”Contact” attribute in the apex:Page tag. Haven’t tested it to be sure. In the blog post example, you could try adding standardController=”Contact” to see if that adds the tab highlighting back.

    Best Regards,

    Mike

  12. Jon LaRosa says:

    Hi,

    For the view to display, I’m trying to get a view for one profile but the standard tab for all others. Here’s my relevant section of code:

    var cView;
    switch ( “{!$Profile.Name}” ) {
    case “Call Center Sales”: cView = “/701?fcf=00BF0000006c06w” ; break ;
    default: cView = “/701/o”; break;
    }

    Problem is that when it tries to direct a user to the “/701/o” url, the override kicks in and it gets stuck in an endless loop.

    I’ve also tried this, but same issue:
    cView = “/701/o?sfdc.override=0″

    Any ideas?

    thanks much,
    Jon LaRosa

  13. Jon LaRosa says:

    Sorry, I solved it with the following:

    window.onload = function() {

    // Change the whole window to point to this location
    // we’ll only get down here for people in the Call Center Sales profile
    parent.document.location.href = “/701?fcf=00BF0000006c06w”;
    }

    Please Wait ….. Loading Campaign Tab

  14. mgsmith says:

    Thanks for sharing!

  15. Jon LaRosa says:

    Looks like not all of my code got posted, so I’ll try it again here. If you still can’t see it all, it’s here:
    http://www.tdcorp.org/misc_testing/redirect_some_users_default_tab_for_others.html

    window.onload = function() {

    // Change the whole window to point to this location
    // we’ll only get down here for people in the Call Center Sales profile
    parent.document.location.href = “/701?fcf=00BF0000006c06w”;
    }

    Please Wait ….. Loading Campaign Tab

    ~ Jon

Leave a Reply