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
Abdul Mujeeb ShaikAbdul Mujeeb Shaik 

Parent to child SOQL Query

how to query all the Contacts,opportunity and opportunitylineitems from Account Object.
I can able to query only contacts and opportunities from Account object.
but i want OpportunitylineItems also in a single query to display in table formats , cany any one help me

This is how i am querying,

SELECT Name,(SELECT name,OpportunityLineItems.name from Opportunities) FROM Account

this is error , but i must query from Account Sobj Only.
BalajiRanganathanBalajiRanganathan
if you want to query Account then you will not able to do this with single query. 
If it is allowed then it will be in this format. SELECT Name,(SELECT name, (select name from OpportunityLineItems) from Opportunities) FROM Account. but currentlty only one level can be selected from the root.

  Go with Two SOQLS.
-  One with contacts and opportunities from Account object 
-   Second with Opportunities and OpportunityLineItems. Map<Id, Opportunity> oppList = new Map<Id, Opportunity>([SELECT name, (select name from OpportunityLineItems) from Opportunities where accounid in:idList]);
- Iterated the first query and set the OpportunityLineItems in each opportunity by getting it from Map
    opp.OpportunityLineItems = map.get(opp.id).OpportunityLineItems;
Abdul Mujeeb ShaikAbdul Mujeeb Shaik

its like bit advanced , 

can you please help be out how to achieve this , @balaji r 

a sample of single record with single columns in each object , please,...

BalajiRanganathanBalajiRanganathan
Ok Try this template below
List<Account> accList = [SELECT id,(SELECT id from Opportunities),(SELECT id from contact) FROM Account where id in : trigger.new];

Map<Id,Opportuniy> oppMap = new Map<Id,Opportuniy> ([SELECT id, (SELECT id from OpportunityLineItems) FROM Opportunity where accountid in : trigger.new]);

for (Account acc : accList) {
  for (Opportunity opp : acc.Opportunities) {
     opp.OpportunityLineItems = oppMap.get(opp.id).OpportunityLineItems;
  }   
}