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
murielmuriel 

Subquery and Map

HI,

I have trying to put the subquery into a second map as so:


Map<id,Case> caseToUpdate = new Map<id,Case>([select id, status,recordtypeid,Response_Time_Remaining__c,(select id , timeremaininginmins  from CaseMilestones where MilestoneType.Name like '%response%') from case where status='new' and recordtypeid='012200000005ZLF']);

Map<id,CaseMilestone> CaseMilestoneMap = new Map<id,CaseMilestone>();

for(Case tempCase:caseToUpdate){

  for(CaseMilestone cm:caseToUpdate.CaseMilestones){
 
    CaseMilestoneMap.put(cm.id,cm);
  }
}



i am getting "Compile Error: Initial term of field expression must be a concrete SObject: Map<Id,Case>" .  It is pointing to "  for(CaseMilestone cm:caseToUpdate.CaseMilestones){" .

i am pretty sure we can retrieve the subqeury and put it in a diff map...

what am i doing wrong? 
Best Answer chosen by muriel
BDatlaBDatla
Hi Muriel,

if you want to loop through values(cases) in the Map<Id,Case> caseToUpdate then below for loop will work.
for(Case tempCase:caseToUpdate.values()){
   ... your logic
}
if you want to loop through keys(Ids) in the Map<Id,Case> caseToUpdate then below for loop will work.
for(Id i:caseToUpdate.keyset()){
   ... your logic
}

You won't be able to loop through map as it is.

Could you please let me know if you need any more help.

Regards,
BDatla

Regards,
BDatla


 

All Answers

BDatlaBDatla
Hi ,

Please use the below code :
for(Case tempCase:caseToUpdate.values()){

  for(CaseMilestone cm:tempCase.CaseMilestones){
 
    CaseMilestoneMap.put(cm.id,cm);
  }
}

Regards,
BDatla
BDatlaBDatla
Hi,

Can you please let me know the issue resolved or not ?

Regards,
BDatla
murielmuriel
Thanks BDatla

looking better

but it is now showing:
Loop must iterate over a collection type Map<Id,Case>

pointing at
for(Case tempCase:caseToUpdate){

might have to review the logic here and see if i can do it differently

 
BDatlaBDatla
Hi Muriel,

if you want to loop through values(cases) in the Map<Id,Case> caseToUpdate then below for loop will work.
for(Case tempCase:caseToUpdate.values()){
   ... your logic
}
if you want to loop through keys(Ids) in the Map<Id,Case> caseToUpdate then below for loop will work.
for(Id i:caseToUpdate.keyset()){
   ... your logic
}

You won't be able to loop through map as it is.

Could you please let me know if you need any more help.

Regards,
BDatla

Regards,
BDatla


 
This was selected as the best answer
murielmuriel
Thanks BDatla

makes sense