You need to sign in to do that
Don't have an account?

Error: Cannot modify a collection while it is being iterated.
Objective: The objective of my batch code is to iterate through a list users and their events. I want iterate through this list of events (which originates from a query which orders the events by time) and, as we iterate through the list, if the event.IsAllDayEvent checkbox is checked/true, then I want that all day event to be the SECOND event in the evtsToReorder collection - not the first. My code below is generating the following error: Error: Cannot modify a collection while it is being iterated.
Not sure why I'm getting this error as I'm out of the for loop when I'm trying to re-order the collection. Any help is appreciated!
-Bronco
CODE:
for (sObject s : scope)
{
User evtOwner = (User)s;
Id evtOwnerId = evtOwner.Id;
if(ownrIdsToEvents.containsKey(evtOwnerId))
{
List <Event> evtsToReorder = new Event[ownrIdsToEvents.get(evtOwnerId).size()];
evtsToReorder = ownrIdsToEvents.get(evtOwnerId);
Boolean userHasAllDayEvent = false;
Boolean reOrderList = false;
Event allDayEvent;
Event zoneRotationEvent;
for(Event ee: ownrIdsToEvents.get(evtOwnerId))//loop through each owners events
{
if (ee.IsAllDayEvent && ee.RecordType.Name != 'Zone Rotation')
{
userHasAllDayEvent = true;
allDayEvent = ee;
}
if(ee.RecordType.Name == 'Zone Rotation' && userHasAllDayEvent)
{
reOrderList = true;
zoneRotationEvent = ee;
Event zoneRotationEventToMove = ee;
break;
}
}
if(reOrderList && userHasAllDayEvent && evtsToReorder.size() > 1)
{
evtsToReorder.set(0, zoneRotationEvent);
evtsToReorder.remove(1);
evtsToReorder.add(1, allDayEvent);
}
}
}
Not sure why I'm getting this error as I'm out of the for loop when I'm trying to re-order the collection. Any help is appreciated!
-Bronco
CODE:
for (sObject s : scope)
{
User evtOwner = (User)s;
Id evtOwnerId = evtOwner.Id;
if(ownrIdsToEvents.containsKey(evtOwnerId))
{
List <Event> evtsToReorder = new Event[ownrIdsToEvents.get(evtOwnerId).size()];
evtsToReorder = ownrIdsToEvents.get(evtOwnerId);
Boolean userHasAllDayEvent = false;
Boolean reOrderList = false;
Event allDayEvent;
Event zoneRotationEvent;
for(Event ee: ownrIdsToEvents.get(evtOwnerId))//loop through each owners events
{
if (ee.IsAllDayEvent && ee.RecordType.Name != 'Zone Rotation')
{
userHasAllDayEvent = true;
allDayEvent = ee;
}
if(ee.RecordType.Name == 'Zone Rotation' && userHasAllDayEvent)
{
reOrderList = true;
zoneRotationEvent = ee;
Event zoneRotationEventToMove = ee;
break;
}
}
if(reOrderList && userHasAllDayEvent && evtsToReorder.size() > 1)
{
evtsToReorder.set(0, zoneRotationEvent);
evtsToReorder.remove(1);
evtsToReorder.add(1, allDayEvent);
}
}
}