+ Start a Discussion

apex class query by count of child records > 1 for parent

Having trouble figuring out the best way (or any way that works for my apex class for that matter) to get the following end result. I can get most of the way there with SOQL and Group by Cube but that doesn't work with apex, and I'm thinking a for loop with a set would be better anyway.

Here's what I'm trying to do - step by step

Find Id, start_date__c, completion_date__c, parentId where parentId has more than one customchild__c record that has null for completion_date__c.

Return list and then from this list - 

I need to create a new list (unless I can do this all in the same initial query which would be great)   - group my previous list by parentId and grab only the customchild__c record from each group where start_date__c is the earliest and return that into a new list.

Help would be much valued.

Hi Jonathanbernd,

If you want the count of child records for a parent.

use this type of query

list<Account>accs=[select id,name ,(select id,name from contacts) from account where id=:'001D000000IRt53'];

for(Account a:accs){
    for(Contact c:a.contacts){
        //do what you want.

Please repalce account and contacts with your parent and child objects.