I would like to update my exsiting code that uses Lists to instead use maps. Here is what I have:
List<object1__c> records1 = new List<object1__c>();
List<object2__c> records2 = new List<object2__c>();
records1 = [SELECT id, name from object1__c ];
records2 = [SELECT id, name from object2__c];
Object3__c result1 = (Object3__c ) so;
if(!records1.isEmpty() && !records2.isEmpty()){
for (object1__c rec: records1) {
for(object2__c rec2: record2)
if (rec.Apple__c == rec2.Pear__c){
rec.Job_Item__c = result1.Id;
}
}
}
I am trying to convert the above to use maps but I am not sure on how to compare the fields between the 2 maps. Would the below code work or Do I need to iterate through both maps? Just trying to reduce the number of for loops:
Map<Id,object1__c> records1 = new Map<Id,object1__c>();
Map<Id,object2__c> records2 = new Map<Id,object2__c>();
Object3__c result1 = (Object3__c ) so;
if(!records1.isEmpty() && !records2.isEmpty()){
for(object1__c rec1 : records1.values()){
if (rec1.Apple__c = records2.getvalues(Pear_c)){
rec1.Test__c = result1.Id;
}
}
}
null
). If you attempt to loop over an empty collection, the loop will see that there's nothing to loop over and automatically skip executing the code inside.