0

We have a requirement in order to Find out the Child Record from the Parent List.

Parent Query: list<Case> caseList = [ select Id, recordTypeId, Type, Sub_Type__c, Origin, WMG_Image_Batch_ID__c, ANN_Company__c,ContactId, WMG_Client_Social_Security_No_Encrypted__c, WMG_Client_Account_Number_Encrypted__c, Product_Manufacturer__c, (Select id From Associated_Documents__r) from Case ];

Is there any way i can get the (Select id From Associated_Documents__r) in separate list as i need for my requirement.

6
  • It already is a list, what exactly is the requirment? Sep 8, 2019 at 7:27
  • Just wanted to calculate the size of (Select id From Associated_Documents__r Which are Present in the caseList ;
    – raghav
    Sep 8, 2019 at 7:34
  • size against each case or total size? Sep 8, 2019 at 7:41
  • Total Size and count of the Associated_Documents__r which are child of Case present in the caseList
    – raghav
    Sep 8, 2019 at 7:48
  • do you know the object api name of Associated_Documents__r? (Associated_Documents__r is child relationship name) Sep 8, 2019 at 8:13

1 Answer 1

0

Find below code and note points:

  1. Count of docs against each case - eachCaseDocsCount
  2. Count of all docs - totalDocs
  3. Count of all docs for caseList - casesDocsCount

Below is the anonymous apex code to find:

list<Case> caseList = [....];
Map<Id, Integer> eachCaseDocsCount = new Map<Id, Integer>();
for(Case cs : caseList){
    eachCaseDocsCount.put(cs.Id, cs.Associated_Documents__r.size());
}
System.debug('eachCaseDocsCount => '+eachCaseDocsCount);

Map<Id, Case> casesMap = new Map<Id, Case>(caseList); // collect map of id to case

Integer totalDocs = [SELECT Count() FROM Case_Associated_Document__c];
System.debug('totalDocs => '+totalDocs);
Integer casesDocsCount = [SELECT Count() FROM Case_Associated_Document__c WHERE Case__c=:casesMap.keySet()];
System.debug('casesDocsCount => '+casesDocsCount);

Assuming that Case__c is the field api name of lookup/master field to Case (If its different, replace it)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .