I am trying to tidy up some really bad legacy code from an old developer and I'm trying to get rid of these nested for loops. The code below shows what I'm currently working with. I've fixed this before with nested SOQL queries but nothing like this. How would this need to look to remove these for loops (except Trigger.New)? Would I use Maps? How would I do that?
map<Id,SObject> mapProperyIdBudDetail = util.GetObjFields(setPropertyId, 'Property');
map<Id,SObject> mapSpaceIdBudDetail = util.GetObjFields( PropertyMap.keyset(), 'Space');
for ( Lease__c l: Trigger.New ) {
if(mapProperyIdBudDetail.get(PropertyMap.get(l.space__c))!=null) {
lProperties = new List<SObject>{mapProperyIdBudDetail.get(PropertyMap.get(l.space__c))};
for ( SObject p: lProperties ) {
for ( String fm: FieldMap.keyset() ) {
if ( p.get(FieldMap.get(fm)) != Null) {
l.put(fm,p.get(FieldMap.get(fm)));
}
}
}
}
}
PropertyMap
? What isFieldMap
? What doesuitl.GetObjFields()
do?), but I'd argue that this code isn't horrible at all. Sure, the variable names are pretty atrocious, but nested loops are not inherently evil. When you're working with generic SObjects, I'd be surprised if you could somehow avoid code that ends up looking at least partially similar to this.