Hit the 5000 record update limit, need help restructuring code
I have a list of addresses that have been given an Area field (San Diego, DFW, Chicago, etc.). These areas include all the cities around the metro of those areas. We want to be able to update which cities belong in the areas, so I have a dynamic form that pulls the information and allows us to add or remove cities for the area. It works perfect for smaller areas, but I hit the 5000 record update limit on larger ones due to nested for loops. As much as I have tried, I cannot think of another way to get the selected cities in the form and update the matching records for the City and State (add or remove the Area for the record based upon it being a selected city in the Area form)
apts = Final_CO_Notes[State == input.State];
x = List();
//Store all selected cities in the form
for each opt in input.Cities
{
x.add(opt);
}
//Go though each record
for each rec in apts
{
//Clear the Area field to update it with new value
if(input.Metro == rec.Area)
{
rec.Area=null;
}
//Loop through selected Cities and if the record city matches update the Area field with the form Area name
for each y in x
{
if(y == rec.City)
{
//Update the record with the new Area that matches the city and state;
rec.Area=input.Metro;
}
}
}