How can I count the number of details in master-detail relationship?
Let us suppose I have Detail_Type__c and Master_Type__c.
Now if I have and
Id of a Master_Type__c record I can do the following query:
List<Detail_Type__c> allDetails = [ SELECT Id FROM Detail_Type__c WHERE Master_Type__c = :Id ]; Integer res = allDetails.size();
But I would like to be able to somehow do the query of the following form:
List<Master_Type__c> allDetails = [ SELECT Master_Type_Field1__c, Master_Type_Field2__c, (SELECT NumberOf Detail_Type__r) FROM Detail_Type__c WHERE Master_Type_FieldDoesNotMatter_c = :something ];
In other words I would like to use something like an aggregate function to count the number of details in master-detail relationship for each detail in a list of details.
How can I do it?