Saturday, June 1, 2019

How to Get List of All Child Records for Given Parent Records in .csv File using Apex

If we have lot of child object for a parent object, then it becomes very difficult to find out list of all related records for a given parent. For example, Account and Contact is reference by many standard and custom objects.

Consider a scenario in which Account is parent for more than 50 objects. In this case if we have to check if account record is being reference in any of these child objects, then how we are going to do that.  Its very difficult to add all related list on account page layout or to write SOQL queries to get this information.

If we use nested queries as mentioned below, then we have limit to use only 20 nested queries.

select id, (Select id from Cases),(Select id from Contacts) from Account where Id='0010000xxxxxxx'

In order to overcome these issues, I have written a script which you can run in developer console and you will receive email with .csv file containing all child records for a given parent record.

In below mentioned script, specify the object API name and 15 or 18 digit record id.

After running this script, you will receive .csv file in email as shown below:

If you are getting below mentioned error, while running the script, the make note of below points:

  • This exception can not be handled by try and catch
  • SOQL is not performing well may be because the large volume of data or due to indexing issues on table.
  • Instead of running this script developer console, use batch apex to run this logic. You can create a custom object and first run a script to store all child objects details like relationship name, child object name. Then you can execute the logic in batch class to first fetch all these records from custom object and then fire nested queries by using relation names and store output in variable. Make sure your batch size is not greater than 20 as we have limit of 20 nested queries in SOQL. In finish method write send email code to get .csv file. If you are still getting this error, then keep on reducing the batch size.
Hope this will help!!

Looking forward for your comments and suggestions.


  1. This comment has been removed by the author.

  2. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. Baby Formula

  3. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. childcare photography in Penrith, Central Coast and Sydney

  4. With a couple of straightforward and simple advances,Infantcore you would have the option to pick the fitting toys for your little kid that too with no trouble.

  5. I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... podiatry drill

  6. Bunk beds are exactly like candy to a child. Nothing is quite similar to climbing up to the top bunk plus feeling as if you're on top of the whole world. bed with slide

  7. Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. impact absorbing surfaces

  8. Thanks For sharing this Superb article.I use this Article to show my assignment in is useful For me Great Work. preschool photographer in Sydney.

  9. Such children regularly endeavor long lasting to be acknowledged and supported by others as a methods for demonstrating to themselves that they are 'alright' and deserving of affection. Having minimal self-esteem, grown-up overcomers of child misuse regularly wind up in careless, even oppressive connections notwithstanding their best goals to discover joy and love. parent help

  10. If you are looking for a mattress that is hypoallergenic, you should first consult a doctor. A great way to get your body to sleep is to have a truck mattress

  11. This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great. Best Large Air Fryers

  12. And assuming that they are in full tone, and of good quality, they are either excessively costly or for an elitist few and past the scope of most African children, particularly those in the rustic regions.
    Diaper Cleaning Services