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
Luca Benedettini 7Luca Benedettini 7 

Apex Error help me please

Hi guys.I need in an apex class a Map of Cases ID with their Attachemnts and a List with that Attachment. Now i write this:
Map<Id, List<Attachment>> AttachMap = new Map<Id,List<Attachment>>([
                           select id,(select id from attachments) from case]);
But i receive the error:
 Invalid initial type List<Case> for Map<Id,List<Attachment>>.
How can i get the Map and after the list of Attachment based on case Id. Please help me 
Best Answer chosen by Luca Benedettini 7
Rohit K SethiRohit K Sethi
Hi Luca Benedettini,

You are quering on Case and this query return the following map :
    Map<id,Case> caseMap = new Map<Id,case>([select id,(select id from attachments) from case]);
    
And for your requirment of AttachmentMap you can use following code :

Map<Id, List<Attachment>> AttachMap = new Map<Id,List<Attachment>>();
    for(Case cs: [select id,(select id from attachments) from case]){
                AttachMap.put(cs.id,cs.attachments);
    }
    

Thanks.

All Answers

Rohit K SethiRohit K Sethi
Hi Luca Benedettini,

You are quering on Case and this query return the following map :
    Map<id,Case> caseMap = new Map<Id,case>([select id,(select id from attachments) from case]);
    
And for your requirment of AttachmentMap you can use following code :

Map<Id, List<Attachment>> AttachMap = new Map<Id,List<Attachment>>();
    for(Case cs: [select id,(select id from attachments) from case]){
                AttachMap.put(cs.id,cs.attachments);
    }
    

Thanks.
This was selected as the best answer
Luca Benedettini 7Luca Benedettini 7
Thank you Rohit, this error droves me crazy