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
Ameer Basha 34Ameer Basha 34 

How to get data from from a variable which contains list of accounts and more than list of 3 child objects records

Hi Everyone,

As I am new to apex development. I met with a requirement where I have an apex class and i have written a soql query  which fetches list of accounts and list of its child records in a variable....There are 4 child objects for accounts....My concern is how to get object wise separate records data from the variable..and set it in wrapper class.I can use for loop inside a for loop but we are minding it as a bad practise with out using nested for loop can any one suggest me any thing..mapping account id to child records is became difficult to me...thanks in Advance....
Danish HodaDanish Hoda

Hi Ameer,
You can refer below code for reference:
In below snippet Child1__c is the object while Child1__r is the relationship name of Child1__c with Account

List<Child1__c> child1s = new List<Child1__c>();
List<Child2__c> child2s =  new List<Child2__c>();

List<Account> accounts = [SELECT Id, Name, (SELECT Id, Name FROM Child1__r), 
								(SELECT Id, Name FROM Child2__r), (SELECT Id, Name FROM Child3__r), (SELECT Id, Name FROM Child4__r) 
						  FROM Account]{
				if(!accounts.Child1__r.isEmpty())	child1s.add(accounts.Child1__r);
				if(!accounts.Child2__r.isEmpty())	child2s.add(accounts.Child2__r);
}