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
Stephanie RhodesStephanie Rhodes 

Resort count Case with attachments

Hi.I am trying to create a report that returns a Count of Cases with Attchments.  Record Type can't do this.  I'm just learning SOQLso I ran a SOQL query but I get a result with a list of all Cases and Attachments.  Here's my query:

SELECT Case.CaseNumber, (SELECT Attachment.ParentID FROM Case.Attachments) FROM Case

I know I am missing Count() but I don't know how to put it in. I just need a simple Count of Cases WITH Attachments. Thanks for helping.
Best Answer chosen by Stephanie Rhodes
Raj VakatiRaj Vakati
Try link this .. 

You cannt able to run Group by in inner SOQL
 
List<Case> cs = [SELECT CaseNumber, (SELECT  ParentID FROM Attachments) FROM Case ];
Map<Id,Integer> casIdstoCoutn = new Map<Id,Integer>(); 

for( Case c :cs){
    casIdstoCoutn.put(c.Id , c.Attachments.size());   
}

 

All Answers

Raj VakatiRaj Vakati
Try link this .. 

You cannt able to run Group by in inner SOQL
 
List<Case> cs = [SELECT CaseNumber, (SELECT  ParentID FROM Attachments) FROM Case ];
Map<Id,Integer> casIdstoCoutn = new Map<Id,Integer>(); 

for( Case c :cs){
    casIdstoCoutn.put(c.Id , c.Attachments.size());   
}

 
This was selected as the best answer
Stephanie RhodesStephanie Rhodes
Thanks Raj.  It says query has to begin with Find or Select. 
Raj VakatiRaj Vakati
You need to execute the code in developer console execute anonymous window 
Stephanie RhodesStephanie Rhodes
That is really cool Raj - thanks!  Just one more question - can I add a date criteria?
Raj VakatiRaj Vakati
You can add .. but what do you mean by  date criteria
Raj VakatiRaj Vakati
Does its wokrs?
Stephanie RhodesStephanie Rhodes
It works Raj.  Thanks so much.