[SalesForce] Hiding home page components

Home page components get added to a homepage layout that are assigned per profile, now if I want to hide the component for specific users in the profile is it possible to do so ?

eg:
sysadmin has user A , user B, user C.

If I have to hide the homepage component (nothing but a IFRAME where src = apex page) for user C and show it only to A and B is it even possible?

Is there some way to hide it using Java script?

I was able to find some reference and got down to this

<div class="iframe">
<iframe src="/apex/page" style="width:1000px;height:400px;"></iframe>
<style type="text/css">
.iframe{display:none !important;} // hide the iframe itself 
h2{display:none !important; //salesforce always uses h2 for custom component headings thus this takes cares of hiding the component name, bad approach if there are other custom components, alternative solutions would be most helpful :) 
</style>
</div>

*the above script works awesome and hides the custom component but i want to scrap the user name of the user from the top of the page and hide the component.*

By inspecting the element i found the class name to be :

**<h1 class="currentStatusUserName"><a href="/_ui/core/userprofile/UserProfilePage">username test user</a>**
</h1>

can someone point me how to scrap the username using jScript to actually compare who logged in and hide the custom homepage component?

Any help will be appreciated.

thanks a lot!!

*UPDATE : Harcoded user Id to make the component only visible for specific users *

Code for refrence :

    <div class="iframe">
    <iframe src="/apex/page" style="width:1000px;height:400px;"></iframe>
    </div>  
    <script>
// moved from the top div and found that the class name for username on the home page as
currentStatusUserName and used  querySelectorAll since IE8 does not understand getElementsByClassName 
    var userid   = UserContext.userId;
    var div_hide_1 = document.querySelectorAll('.iframe')[0];
    var div_hide_2 = document.getElementsByTagName('h2');

    if(userid   == 'someuserid'){
    }
    else{
        for(i=0;i<div_hide_2.length;i++){
            if(div_hide_2[i].innerHTML == 'Component Test'){
                div_hide_1.style.display = 'none';
                div_hide_2[i].style.display  = 'none';
            }
        }
    }
    </script>

Best Answer

Messy. Real shame that stuff like {!$User.Username}, {!$UserRole.Name} doesn't work in the HTML components.

If you have to scrape - try to do it by user id, not by name of the user. I've recently fixed an interesting bug where one trigger didn't work for Hungarian users - trigger was looking for FirstName LastName and Hungarian locale flips the order like in Asian languages ;)

There's one javascript variable you might reuse without all the hassle of traversing DOM:

var id = UserContext.userId;
console.log(id);

Combine with an array with user Ids and you're getting somewhere. I'd be tempted to explore cookies route too (kind of user preferences) but that means you'd have to assign them somehow in pure JS (cookie served from Visualforce will come from different domain) and you'd need to think how to set cookie if null before relying on its' value so it works ok when new user / new browser is used...

Is AJAX really banned? One query for example to test Group membership (or flag on User), set the cookie and subsequently rely only on the cookie could work and be fairly OK in maintenance.


If you can live with having ugly placeholders in the sidebar use <apex:page rendered="false">. Put a boolean condition there (if your page has a controller it can be as complex as you need) and on the home page the iframe will still be displayed, just with blank content.

Related Topic