function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AKallAKall 

How to get context user in a controller variable.

I want to get the context user of a page for which I am writing a controller because I want the page to appear one way for a certain type of user and another for a different type of user.

 

This is an example of what I had in mind.

 

String showForm = ($User.Role.Name = 'License Control') ;

 

There seems to be something wrong with my syntax, and I am having trouble finding a solution in the documentation. 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

Check the user in your controller and not the page. Then hide/show different parts of the page based on the user.

 

In your controller you can get the user data this way.

 

public MyCustomController(){ Id userRoleId = UserInfo.getUserRoleId(); UserRole theUsersRoleName = [select Id, Name from UserRole where Id = : userRoleId Limit 1].Name; }

 Check out the UserInfo Methods in the Apex Docs.