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
Dave ShulmanDave Shulman 

Retrieving child records to list from parent record

Looking back at some of the existing code in my org someone used something like this to get child records onto a list
for(Account acc : accounts){
            List<Sales_data__c> salesDatas = acc.Sales_data1__r;
I had no idea you were able to do it like this without a query, but now when i try to repurpose a version of this somewhere else, it isnt pulling working when everything is virtually the same? Please advise thanks!
Best Answer chosen by Dave Shulman
David Zhu 🔥David Zhu 🔥
At the code above these lines, there must be a soql to get the account list. It should be similiar to this:

accounts = [select id,name, (select xxxx from Sales_data1__r) from Account where ........];

Basically, it pulls all sales_data1__c records associated to the correspoding accounts by matching account id with  Account__c lookup (or master-detail)_ relationship field value.