I have an ID of user(say 00536000002qYTUIU8), which is not necessarily current context user, and want to get that user's name in apex. I can do following to get this:

select name from User where id ='00536000002qYTUIU8'

But is there any way to avoid querying DB and get the name from any method, something like:

  • If the user trigger the apexcode, UserInfo.getUserName ( don't remember the exact function from UserInfo) will return his name. Else I think it's the only way
    – Simonp
    Commented Jan 25, 2017 at 6:36
  • 1
    @SimonPlacentino UserInfo class is for the current user only.
    – Lance Shi
    Commented Jan 25, 2017 at 6:38
  • @SimonPlacentino : Yes, I want it for any random user.
    – Sarang
    Commented Jan 25, 2017 at 6:38
  • You have to query, otherwise, no ready made API is available Commented Jan 25, 2017 at 6:39
  • 1
    @Rangya You are right, that is why I specified "If the user trigger the apexcode". You could do your own class to hide the SOQL query. Create a UserUtility class with a static method getUserName() to do so
    – Simonp
    Commented Jan 25, 2017 at 6:57

1 Answer 1


One option to reduce the SOQL call count for data you need frequently is to use the Platform Cache.

You could store the User ID to name lookup in the cache, and would then only need to do a SOQL query if the cache missed.

How effective this is will depend on how many User records you are dealing with and how much storage space you can allocate to the Cache.

You could also do an API call to get the data. Either to the REST API or the SOAP API. This would technically save you the SOQL query, but would use a callout instead. The other big downside of this approach is the performance. A callout will always be much slower than a direct SOQL query.

  • 1
    You need to register your organisation as "Remote Site" in Setup -> Security -> Remote Site Settings. With your current SessionId: Http http = new Http(); HttpRequest request = new HttpRequest(); String instance = Url.getSalesforceBaseUrl().toExternalForm(); String endPoint = instance + '/services/data/v38.0/sobjects/User/0050Y000000pmuYQAQ?fields=Name'; String sessionId = 'Bearer ' + UserInfo.getSessionId(); request.setMethod('GET'); request.setHeader('Authorization', sessionId); request.setEndpoint(endPoint); http.send(request).getBody();
    – Simonp
    Commented Jan 25, 2017 at 7:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .