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
sroberts_MEsroberts_ME 

Current users account Id

Hi all,

 

I'm trying to figure out how to find the Id of the account the current user is associated with. I want to limit a query to find only records associated with that account. I thought there was a $User.account or something, but there doesn't seem to be. Any ideas?

 

Thanks,

Sam

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

No there is no direct way of this,

 

You have too fetch it from contact for current user.

 

ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;

ID AccID  = [Select AccountID from Contact where id =: contactid].AccountId;

All Answers

NasipuriNasipuri

Hi Sam,

 

A user can be associated with an Account (I think you mean to say Standard Account Object) by several ways.

 

User can be the Owner of the Account

Can be the Creator of the Account

Can be the Last modified by person.

 

There can be some custom relationship, based on the system configuration as well.

 

Based on your business login you need to develop the SOQL and need to put LIMIT.

 

There are no such user global static properties to access the Account as there relationship is not of that type.

 

Thanks,

Dinesh Nasipuri

dinesh.nasipuri@gmail.com



Shashikant SharmaShashikant Sharma

No there is no direct way of this,

 

You have too fetch it from contact for current user.

 

ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;

ID AccID  = [Select AccountID from Contact where id =: contactid].AccountId;

This was selected as the best answer
sfdeveloper9sfdeveloper9

try

 

[Select contact.AccountId from user where id =: Userinfo.getUserid()];

Joshua Carritt-Baker 2Joshua Carritt-Baker 2
You can do it. See https://help.salesforce.com/articleView?id=siteforce_communities_data_code_reference.htm&type=5
Dheeraj.PandeyDheeraj.Pandey

You can use 

string strAccId = [select accountId from user where id=:userinfo.getuserid()];