In the code below, I get an error on the bolded line. Says Loop must iterate over a collection type: Map>

Any idea how to troubleshoot?

Map<Id, List<Event>> events = new Map<Id, List<Event>>();
for (Event record : [
    SELECT WhoId FROM Event
    WHERE WhoId IN :contactIds
    AND WhatId = null
    AND Type = 'MQL'
    if (!events.containsKey(record.WhoId))
        events.put(record.WhoId, new List<Event>());

for (Opportunity record : trigger.new)
    if (!ocrs.containsKey(record.Id)) continue;
    for (OpportunityContactRole ocr : ocrs.get(record.Id))
        if (!events.containsKey(ocr.ContactId)) continue;
        for (Event event : events.get(ocr.ContactId))
            event.WhatId = record.Id;
List<Event> eventsToUpdate = new List<Event>();
**for (List<Event> contactSpecificEvents : events)**
update eventsToUpdate;
  • put bit details about what you are trying to achieve – Santanu Boral Feb 13 '17 at 23:43
  • 1
    Loop variable needs to be events.values(). – Adrian Larson Feb 13 '17 at 23:46
  • @brooksbruce can you please check my ans and close this question – Santanu Boral Feb 14 '17 at 15:40
  • Sorry. For some reason it wouldn't let me check your answer yesterday. There was a time limit I had to wait for. Thanks for the help. – Brooks Bruce Feb 14 '17 at 18:15

Since you have declared Map<Id, List<Event>> events = new Map<Id, List<Event>>();

So, to loop through the List of events you need to use events.values()

for (List<Event> contactSpecificEvents : events.values())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.