You need to sign in to do that
Don't have an account?

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??
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!!
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
agree with Yuchen, and also you need to check the following thing,
--> This field is null for Salesforce users.
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.
string contid='select contactid from user where id = '+ Uid + '';
I think you are not using the things correctly.
List<User> userList = [select contactid from user where id =:UserInfo.getUserId()];
List<accountcontactrole> acr =[select accountid,Account.name from accountcontactrole wherecontactid =:userList[0].contactId];
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!!
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];
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!!
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
}