I'm trying to frame a single SOQL on opportunity with fields from Multiple objects like Account Name, Contact Name ,Contact Ph , Contact Email and the Opplineitems. I'm not sure how to get the Account Name and Contact Details from opp which has lookup.

 Opportunity Op=[select id,Name,Stagename,Account__r.Name,(select Product_Name__c,Bid_Type__c from OpportunityLineItems) from opportunity where stagename='Closed-Won' and id=:opptyid];
  • 1
    helpful tip -- OOB SFDC relationships never use __r; OOB SFDC fields never use __c. Hence relationship from Opportunity to Account is simply account.someAccountOOBfieldname or account.someAccountCustomfieldname__c
    – cropredy
    Aug 21, 2015 at 19:40

1 Answer 1


Unless you have a contact related to the Opp via a lookup then you will have to do two queries. (or a subquery on via Contact Roles child object on Opp if that fits your need) For Account use


From the query on Opp

Since you have a lookup to contact, use the API name replacing the __c with __r and add .name or whatever other field you need

  • We have lookup to Contact from opportunity, I need to get the details of this contact. Aug 21, 2015 at 15:34
  • The use the Contact__r.Name (Or whatever the API name is of the Lookup to the contact
    – Eric
    Aug 21, 2015 at 15:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .