5

I have a controller extension and want to use the Tabbed view to get rid of the Chatter caused endless scrolling.

    public class UserProfileGetter {
    public UserProfileGetter(ApexPages.StandardController controller) {

    String userProfileName = [select u.Profile.Name, id, UserType from User u where u.id = :Userinfo.getUserId()].Profile.Name;

    system.debug('Profile===================='+userProfileName);

    }

  }

When I run the debug it shows my Profile as 'System Administrator' correctly.

My VF page code:

<apex:page standardController="Account" extensions="UserProfileGetter" showHeader="true" tabStyle="account" >

 <apex:tab label="Opportunities" name="Opportunities" id="tabOpp" rendered="{userProfileName!='Chatter Content'}">
         <apex:relatedList subject="{!account}" list="opportunities" />
      </apex:tab>
       </apex:tabPanel>
</apex:page>

So the logic for render I want is - if the user profile name isn't Chatter Content, render. But if I have this logic in the page it won't render for anyone, including me. Ideas? Thanks

Amending - to query other variable on the user, a checkbox called "Admin Team" to display the tab for Account Teams related list, I query for both like this:

public class UserProfileGetter {
    public String userProfileName {
        get {
            return [
                    select Profile.Name
                    from User
                    where Id = :Userinfo.getUserId()
                    ].Profile.Name;
        }
    }
    public boolean userAdminTeam {
        get {
            return [
                    select Admin_Team__c
                    from User
                    where Id = :Userinfo.getUserId()
                    ].Admin_Team__c;
        }
    }

    public UserProfileGetter(ApexPages.StandardController unused) {
    }


}

And then I use like this:

 <apex:tab label="Account Team" name="Account Team" id="Act" rendered="{!userAdminTeam}">

Thanks all.

3 Answers 3

10

You don't need to write any apex code for this.

In your vf page, all you need is, rendered="{!$Profile.Name=='Chatter Content'}"

Hope that helps.

3

You need a public property in the controller:

public class UserProfileGetter {
    public String userProfileName {
        get {
            return [
                    select Profile.Name
                    from User
                    where Id = :Userinfo.getUserId()
                    ].Profile.Name;
        }
    }
    public UserProfileGetter(ApexPages.StandardController unused) {
    }
}

for the page to be able to access the value and you are missing a ! in the page:

rendered="{! userProfileName != 'Chatter Content' }"
4
  • I'd appreciate some explanation of the reason for the downvote on this answer as it is correct and precedes the @dphil answer.
    – Keith C
    Mar 4, 2014 at 21:40
  • thanks Keith seems to be working. In my example I had taken out the ! b/c I was running different scenarios. Also now I am thinking ahead - how can I do this with multiple profiles...
    – jaw999
    Mar 6, 2014 at 15:28
  • 1
    I'm guessing someone didn't like how it would run the query each time that section of the page got refreshed. That's the only thing I could think of. Maybe something else. Idk.
    – dphil
    Mar 6, 2014 at 19:17
  • 1
    @dphil Yeah usually I'd include a "userProfileName == null" guard in the getter to be sure. But whenever I've checked, no matter how many references to a property there are in the page, the property is only referenced once i.e. some sort of caching is used.
    – Keith C
    Mar 6, 2014 at 19:50
1

Your variable needs to be accessible on the page:

public String userProfileName{get;private set;}

public UserProfileGetter(ApexPages.StandardController controller) {
  userProfileName = [select u.Profile.Name, id, UserType from User u where u.id = :Userinfo.getUserId()].Profile.Name;

You are then missing the first ! on the rendered:

rendered="{!userProfileName!='Chatter Content'}"

Optionally (my preference):

public Boolean isChatter{get; private set;}

    public UserProfileGetter(ApexPages.StandardController controller) {
          isChatter = ([select u.Profile.Name, id, UserType from User u where u.id = :Userinfo.getUserId()].Profile.Name == 'Chatter Content');

then:

rendered="{!isChatter}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .