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
Linda 98Linda 98 

How to query contactid of logged in user??

Hi

I am trying to query contactid of logged in user like this.
But i dont see any value.
Where am i going wrong??
 
Id Uid=UserInfo.getUserId();
//DEBUG HERE GETS LOGGED IN USER ID
string contid='select contactid from user where id = '+ Uid + '';
//DEBUG HERE IN CONSOLE SHOWS 1 record BUT VALUE SEEMS TO BE NULL.
List<accountcontactrole> acr =[select accountid,Account.name from accountcontactrole where contactid =:contid];
//DEBUG HERE IS EMPTY LIST.
Thanks in advance!!
 
Best Answer chosen by Linda 98
Pankaj_GanwaniPankaj_Ganwani
Hi,

You will have to iterate on the inner soql:

Contact objContact = [select AccountId,(select AccountId from AccountContactRoles) from Contact where Id=:contactId];
system.debug('==='+objContact.AccountId);//This will give you parent account

for(AccountContactRole obj: objContact.AccountContactRoles)
{
     system.debug('=================='+obj.AccountId);// This will give you the account, contact has a role with
}

All Answers

YuchenYuchen
So for the current logged in user, is there a Contact value associated with it? Also, could you print the "contid" in debug logs and see what it returns?
sreenivasAppssreenivasApps
Hello,

agree with Yuchen, and also you need to check the following thing,

--> This field is null for Salesforce users.
Pankaj_GanwaniPankaj_Ganwani
Hi,

Is the currently loggedin user a portal or community user? If so, then only you can get the ContactId else it will be null for sys admin, std user etc. profiles.
pankaj kabra 8pankaj kabra 8
What this line is doing here

string contid='select contactid from user where id = '+ Uid + '';

I think you are not using the things correctly.
pankaj kabra 8pankaj kabra 8
Use in this way


List<User> userList = [select contactid from user where id =:UserInfo.getUserId()];
List<accountcontactrole> acr =[select accountid,Account.name from accountcontactrole wherecontactid =:userList[0].contactId];
Linda 98Linda 98
Thanks Pankaj Kabra 8 and all.
That worked.
But i am not able to get my requiremnt.
I want to query all accounts on which my contact(logged in user)  is having contact role too.
EG:
I have two accounts A,B and one contact C

C is contact of account A but has contact role on ACCOUNT B
So this is what i want to query,i have to get account A and B on my account look up field.

I am following this blog.But struck here with the query..

http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/

please help!!

 
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can use below mentioned SOQL to fetch all accounts which includes parent account as well as those accounts the contact has a role with:
[select AccountId,(select AccountId from AccountContactRoles) from Contact where Id=:contactId];
Linda 98Linda 98
Thanks Pankaj.
But it didnt work.I am only getting one account one which contact is.As mentioned in above example,i am only getting Account  A.

I even want account B as i am having account contact role on it...Please guide me!!
Pankaj_GanwaniPankaj_Ganwani
Hi,

You will have to iterate on the inner soql:

Contact objContact = [select AccountId,(select AccountId from AccountContactRoles) from Contact where Id=:contactId];
system.debug('==='+objContact.AccountId);//This will give you parent account

for(AccountContactRole obj: objContact.AccountContactRoles)
{
     system.debug('=================='+obj.AccountId);// This will give you the account, contact has a role with
}
This was selected as the best answer