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
NLTNLT 

child records count for parent to run it in anonymous block

Hi,

Can someone please provide me logic to get child records count for parent to run it in anonymous block.

 
SwethaSwetha (Salesforce Developers) 
HI Lakshman,
You can try something like 
List<User> users = [SELECT Name, (SELECT id FROM Applications__r) FROM User];
for (User u : users) {
    // do something with it...
    u.Applications__r.size();
}
See related https://salesforce.stackexchange.com/questions/36690/soql-count-number-of-child-records

If this information helps, please mark the answer as best. Thank you
mukesh guptamukesh gupta
Hi Lakshman,

Please follow below code
List<Account> acc = [SELECT Name, (SELECT id FROM Contacts__r) FROM Account];
for (Account a : acc) {
   
    system.debug('Total contacts :- '+a.Contacts__r.size());
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh