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
alana_alana_ 

Get salesforce logged in user

Hello,

 

I am using the OpenCTI API. One of the methods I could use to possibley obtain the logged in user of Salesforce is the method 'runApex'. So, I created an Apex class below. However, I am unable to get the user. Does anybody know how to successfully obtain the salesforce user from the Apex class. Any ideas?

 

global class GetCurrentUser{

    webService static String getUserName(String name) {
        System.debug('User Id: ' + UserInfo.getUserId());
        return UserInfo.getUserId();
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
MJ Kahn / OpFocusMJ Kahn / OpFocus

Once you have the User Id (from UserInfo.getUserId()), you can use it to query the User object:

 

User u = [select Id, username from User where Id = :UserInfo.getUserId()]

All Answers

MJ Kahn / OpFocusMJ Kahn / OpFocus

Once you have the User Id (from UserInfo.getUserId()), you can use it to query the User object:

 

User u = [select Id, username from User where Id = :UserInfo.getUserId()]
This was selected as the best answer
ibtesamibtesam

+1 MJ

 

Once you get the id, following information can be queried using SOQL query.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_user.htm

alana_alana_

Thank you all for your response. It works.

Mitesh JainMitesh Jain
As the question was posted way earlier and salesforce have made lot of changes. Just to inform everybody we can now directly use Userinfo.getUsername(). 

Refer : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_userinfo.htm

Thanks.
Yash Modi 27Yash Modi 27
I've tried this, but keep getting Platform Integration User. I am trying to find logged in user's name for a personalised greeting on the Einstein Chatbot. When I run the same code in the developer console, I can see my name being returned. Is there a workaround?
pankaj sfcloudspankaj sfclouds
Try this,
String userName  = UserInfo.getName();
system.debug(userName);