Visualforce: How to display user's account using the $User

Richard N picture Richard N · Aug 1, 2012 · Viewed 8k times · Source

I have a visualforce component which I use for displaying the user's name. Currently I am doing it using the following:

!$User.FirstName} {!$User.LastName}

Is there any way I can display the User's Account as well using this without writing SOQL. I would not like to create a controller just to display the user's account and am hoping there is a simpler and easier way using just Visual force.

I did try !$User.Contact.Account}

But it gave me an error.

Thanks.

Answer

Matt K picture Matt K · Aug 1, 2012

Here are two things you may want to try:

  1. Reference the Account Name from the $User Visualforce global variable.

    {!$User.Contact.Account.Name}

  2. If that doesn't work, create a new formula field on the User object named Contact Account Name with this formula: Contact.Account.Name. Once you have created that formula field, you should be able to reference it in Visualforce using: {!$User.Contact_Account_Name__c}.