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
SurjenduSurjendu 

How to get the email address of the logged in user?

I know i can do the following to get userid,firstname,lastname by using the static UserInfo class. But there is no method to get Email address. How do i get email address in a controller??

  String userId = UserInfo.getUserId();
String firstName = UserInfo.getFirstName();
String lastName = UserInfo.getLastName();

I know i can do {!$User.Email} but that would only be in the visualforce pages.
How do I get the email address of the logged in user in controller.


mikefmikef
At this time I don't see a getUserEmail() method for the UserInfo static class.

The workaround is to use a query to get back the email from the username.


Code:
String userName = UserInfo.getUserName();
User activeUser = [Select Email From User where Username = : userName limit 1];
String userEmail = activeUser.Email;

 
Hope this helps

philbophilbo
Use the userId, not the userName, if you want it to be completely deterministic.

-philbo
mikefmikef
philbo:

I would agree with you on all other cases that the Id is better except this one.
And in this one it's either or.

Usernames in salesforce.com have to be unique.

philbophilbo
that's correct!  (of course)
Chandan3955Chandan3955

Thanks..

It helped me..

Cheers..
 

d.sureshkumar41.33978150573839d.sureshkumar41.33978150573839

How to get the all of the logged in users for particular salesforce account? PLZ help me

 

praveen murugesanpraveen murugesan
 

Hi,

Through this we can get the email of the current loggedin user.

UserInfo.getUserEmail()

Thanks

Praveen Murugesan

 
ADARSH SINGH 17ADARSH SINGH 17
in spring 13 New Apex method is introduced.

UserInfo.getUserEmail()

work fine.
Wes Reed 27Wes Reed 27
yes: UserInfo.getUserEmail() works perfect
Ramssf70Ramssf70
I tested with developer organisation it is working perfectly.we can use that method UserInfo.getUserEmail() method to get current loginuser