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 

SOQL to get account id related to my contact and account contact role's

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!!
Alex Edwin 8Alex Edwin 8
With my understanding, AccountContactRole doesnt seem to have any relationship with Account or Contact.

May be you can run another SOQL on AccountContactRole.
 
// prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);  
    results.add(performRoleSearch(searchString)); //Write your AccountContactRole SOQL here.            
  }
JayantJayant
You may not do it in a single query and bind its results to a table on your over-ridden lookup page. You would need to query both Contact and AccountContactRole objects and then merge the AccountIds in a list. You may then use this list to query Accounts (if you need any more Account fields besides Id, else you may include Account fields in same queries too like Account.Name, Account.Site etc., but extracting this info from query results would be far more complex then simply consolidating Account Ids and querying Account object directly). You may bind this list (query result for Account object) to the table on your over-ridden lookup page.

Queries may be like - 

contactAccounts = [SELECT AccountId FROM Contact WHERE Id = :con.Id];
contactRoleAccounts = [SELECT AccountId FROM AccountContactRole WHERE ContactId = :con.Id];

or

contacts = [SELECT AccountId, (SELECT AccountId FROM AccountContactRole) FROM Contact WHERE Id = :con.Id];


If this is for a portal user (as your post suggests), you may first want to obtain the Id of Contact associated to the logged in user - 

conId = [SELECT ContactId, AccountId FROM User WHERE Id = :UserInfo.getUserId()].ContactId;

In this case you don't need to query Contact object again as the query on User object can provide the parent Account's Id too. So you will need to query on User and AccountContactRole instead.
JayantJayant
If this is resolved, please do mark the question as Resolved and the most appropriate/helpful answer as the best answer :).
If none of the answers helped you significantly, please post the solution. You may also mark your solution as the best answer.