+ Start a Discussion
Patrick Marks 2Patrick Marks 2 

Soql query that pulls Contacts from a custom Account Lookup field

Hi all-

We have a custom field on Opportunities called "Referring Account", which is a lookup field related to Accounts. Is it possible to run a query on Opportunities that pulls in every Contact associated to the Account in this custom lookup field (Referring Account) and if so, how would you go about writing it? Thanks!
Raj VakatiRaj Vakati
Not in single SOQL 

Like below 
 

Map<Id ,Opportunity> lstOppts = new Map<Id,Opportunity>([select id,AccountId From Opportunity WHERE Id=:oppId]); 

Set<Id> oppsIds 

select id, Name, (Select Id, Name from contacts), (Select Id, Name From Opportunities where Id=:lstOppts.keySet()) from Account


OR 
 
SELECT Name...,
       (SELECT Name... FROM Contacts),
       (SELECT Name, Amount, CloseDate... FROM Opportunities WHERE Id = :oppId)
FROM Account
   WHERE Id IN (SELECT AccountId FROM Opportunity WHERE Id = :oppId)

 
Patrick Marks 2Patrick Marks 2
Hi Raj-I tried both of these methods and didn't have any luck.