2

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>());
    events.get(event.WhoId).add(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)**
{
    eventsToUpdate.addAll(contactSpecificEvents);
}
update eventsToUpdate;
}
4
  • put bit details about what you are trying to achieve Commented Feb 13, 2017 at 23:43
  • 1
    Loop variable needs to be events.values().
    – Adrian Larson
    Commented Feb 13, 2017 at 23:46
  • @brooksbruce can you please check my ans and close this question Commented Feb 14, 2017 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. Commented Feb 14, 2017 at 18:15

1 Answer 1

5

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())
{
    eventsToUpdate.addAll(contactSpecificEvents);
}

You must log in to answer this question.

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