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
RossGRossG 

Retrieve userId without SOQL in apex class

Hi,

I am having a problem that should be really easy to solve.  It's easy to retrieve a user id in a trigger or class using soql.  But I am hitting a soql 101 limit error and thus need to have my code run some of one of my trigger when a specific user creates a record.  I can also do this by simply hardcoding the user's id in the trigger, but I don't want to do that.

Basically the algorithm is: trigger on object x updates a few fields on a record after it is interted, but only when user "a" creates the record.  The trigger should simply call a class that updates some fields on that newly inserted record by that specific user.  The trick is, in the trigger, specify the user who should be allowed to call that class without hardcoding the user's ID.

Basically what I need is a utiluty class I can call that returns the userId of the current user based on his/her alias or username.  Any ideas? 
AlgoworksAlgoworks
Hi RossG.. you can get user details from user info class like UserInfo.getuserId() please vist this link for more understanding of the UserInfo class https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_userinfo.htm
Nirmal ChristopherNirmal Christopher
using id u=userinfo.getuserid(); we can retrive the id of current logged in user in a trigger or a class without using a SoQL query. In visualforce page using global declaration you can direcly use like this <apex:outputtext value={!$user.name} > id or  any fields u can reteive from user object. mark this as best ansers if it helps.
Abhi_TripathiAbhi_Tripathi
Hey,

Just use this

//Returns current logged in user Id
UserInfo.getUserId();

Regards,
Abhi Tripathi
Salesforce Developer