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
AgendumAgendum 

Accessing User.FirstName in a Controller


Hi, I come from C# land and am new to Apex.  I am simply trying to get access to the User.FirstName value in the controller.  I can do this in the view just fine:

{!$User.FirstName}

Below are examples of my attempts at doing this...

// In the view:
// -----------------

{!test}

// In the controller:
// -----------------

// Compiler error: Return value must be of type: String
public static string test { get { return User.FirstName; } }

// Compiler error: Incompatible types since an instance of Schema.SObjectField is never an instance of String
public static string test { get { return (string)User.FirstName; } }

// Compiles but always returns 'FirstName'
public static string test { get { return User.FirstName + ''; } }

// Compiler error: expecting a semi-colon, found ':'
public static string test { get { return :User.FirstName; } }

// Compiler error: line 33:10 no viable alternative at character '$'
public static string test { get { return $User.FirstName; } }

Surely there is a way to do this?

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You can use

 

UserInfo.getName() , UserInfo.getFirstname() , Userinfo.getLastName();

 

All these ale User Info methods

 

You can use it like this in property

 

 

public static string test {
      get {
                return UserInfo.getFirstname() ;
          }         
       }

 

 

All Answers

Shashikant SharmaShashikant Sharma

You can use

 

UserInfo.getName() , UserInfo.getFirstname() , Userinfo.getLastName();

 

All these ale User Info methods

 

You can use it like this in property

 

 

public static string test {
      get {
                return UserInfo.getFirstname() ;
          }         
       }

 

 

This was selected as the best answer
Rahul S.ax961Rahul S.ax961

I believe by using User.FirstName you are referring to Currently Logged in User's First Name.

If yes then, Try this :

 

 

public static string test { get { return Userinfo.getFirstname(); } }

 

SS - :P

calvin_nrcalvin_nr

What if we need to obtain the email which is present under $User.