I want to bring details of two objects in a single query. suppose Acccount is parent and I want to bring details of Contact and Opportunity in a single query. Is it possible to fetch the data in inner query of both contact and opportunity.


Yes you can as in below query.

List<Account> accountsWithChildren = [SELECT Id, Name, (SELECT Id, Name FROM
Opportunities), (SELECT Id, Name FROM Contacts) FROM Account];

If you need to refer the children in apex, you can use sample like,

for(Account a : accountsWithChildren){
    for(Contact c : a.Contacts)
        System.debug('contact : ' + c.Name);
    for(Opportunity o : a.Opportunities)
        System.debug('opportunity : ' + o.Name);
|improve this answer|||||

Yes it is possible

SELECT Id, Name, (SELECT Id, LastName FROM Contacts), 
                 (SELECT Id, Name FROM Opportunities) 
  FROM Account
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.