Activity Forums Salesforce® Discussions How to get the userId of currently logged user in salesforce?

  • Ankit

    Member
    March 20, 2018 at 2:12 pm

    Hi Kapil,

    You can get the ID’s of all the currently logged in users by using this global function: UserInfo.getUserId().

    Thanks

  • Adarsh

    Member
    March 22, 2018 at 12:44 pm

    Hi Kapil,

    For apex Class, you may use and UserInfo.getUserId(), and for a Visualfroce page, you may use {!$User.Id}.

    Hope it helps 🙂

  • PRANAV

    Member
    March 23, 2018 at 11:14 am

    Hi,

    You can use userinfo class in apex to obtain the information related to logged in user.

    You can get Id using userinfo.getUserId() and if userinfo class is not able to provide info use query to query those fields .

    On VF you will need to use

    <apex:outputText value="{!$User.Id}">
    You are using outputfield and hence you are not able to save the code

    Edit::

    A sample getter setter User object type variable will be handy

    public class MailExtension{
    public user currentuser{get;set;}
    public MailExtension(ApexPages.StandardController cont){
    opp =(Opportunity) cont.getRecord();
    currentuser=new User();
    currentuser=[Select Id,Name,Email from User where Id=:userinfo.getuserId()];
    }
    }
    A sample VF tag or mark up will be as follows

    <apex:outputfield value="{!currentuser.Id}"/>
    <apex:outputfield value="{!currentuser.Email}"/>

    Hope this helps you.

  • Parul

    Member
    September 18, 2018 at 1:49 pm

    Hi Kapil,

    There is different way to get the userId  -:

    In apex -: Use UserInfo.getUserId()

    In VisualForcePage -: Use  {!$User.Id}

    Thanks

Log In to reply.

Popular Salesforce Blogs