0

I have the following piece of source code:

List<Account> accountsList = ([SELECT id FROM Account]); 
for(Account account: accountsList) { 
    List<CustomObject1__c> allCustomObjects1 = ([SELECT Id, Account FROM CustomObject1__c where AccountName =:account.Id]); 
    List<CustomObject2__c> allCustomObjects2 = ([SELECT Id, Account FROM CustomObject2__c where AccountName =:account.Id]);
    for(CustomObject2__c cObj2 :allCustomObjects2) { 
        List<CustomObject3__c> allCustomObject3ForCobj1 = new List<CustomObject3__c>(); 
        List<CustomObject3__c> allCustomObject3ForCobj2 = cObj2.CustomsObject3__r; 
        if(allCustomObjects1.size() == allCustomObject3ForCobj2.size()) { 
            continue; 
        } 
        if(allCustomObject3ForCobj2.size() > 0) { 
            delete allCustomObject3ForCobj2; 
        } 
        for(CustomObject1__c cObj1 :allCustomObjects1) { 
            CustomObject3__c cObj3 = new CustomObject3__c(); 
            cObj3.CustomObject1__c = cObj1.Id; 
            cObj3.CustomObject2__c = cObj2.Id; 
            allCustomObject3ForCobj1.add(cObj3);  
        } 
        insert allCustomObject3ForCobj1; 
    } 
}

How can I avoid nested for loops and use Map instead?

3
  • 2
    Can you describe in words what this piece of code is intended to do? It appears to be creating junction objects between all records in one object and all records in another, but the logic is a bit confusing, and your anonymization of the code has made it incorrect in several places.
    – David Reed
    Jan 22, 2019 at 12:48
  • Code was edited and made correct.
    – lemdev
    Jan 22, 2019 at 13:18
  • @DavidReed CustomObject3__c cObj3 is a junction object for CustomObject1__c cObj1 and CustomObject2__c cObj2. cObj1 must me added to cObj3
    – lemdev
    Jan 22, 2019 at 13:42

1 Answer 1

3

Here is the example how to do it this is not complete solution but give you an idea how to do it. Please don't do DML or soql inside for loop create a list and do that action outside of the loop.

Map<ID, Account> accountMap = new Map<ID, Account>([SELECT Id, Name FROM Account LIMIT 50000]);
List<CustomObject1__c> allCustomObjects1 = ([SELECT Id, Account FROM CustomObject1__c where AccountName IN:accountMap.keySet()]); 
List<CustomObject2__c> allCustomObjects2 = ([SELECT Id, Account FROM CustomObject2__c where AccountName  IN:accountMap.keySet()]);

Map<Id,List<CustomObject1__c>> object1map = new Map<Id,List<CustomObject1__c>>();

for(CustomObject1__c obj1 : allCustomObjects1){
    if(object1map.get(obj1.AccountName) == NULL){
        List<CustomObject1__c> tempList = new List<CustomObject1__c>();
        tempList.add(obj1);
        object1map.put(obj1.AccountName,tempList);
    }
    else{
        List<CustomObject1__c> tempList = object1map.get(obj1.AccountName);
        tempList.add(obj1);
        object1map.put(obj1.AccountName,tempList);
    }
}
Map<Id,List<CustomObject2__c>> object2map = new Map<Id,List<CustomObject2__c>>();
for(CustomObject2__c obj2 : allCustomObjects2){
    if(object2map.get(obj2.AccountName) == NULL){
        List<CustomObject2__c> tempList = new List<CustomObject2__c>();
        tempList.add(obj2);
        object1map.put(obj2.AccountName,tempList);
    }
    else{
        List<CustomObject2__c> tempList = object2map.get(obj2.AccountName);
        tempList.add(obj2);
        object1map.put(obj2.AccountName,tempList);
    }
}

for(Id accountId : accountMap.keyset()){
    List<CustomObject1__c> obj1List = object1map.get(accountId);
    List<CustomObject2__c> obj2List = object2map.get(accountId);
    // ==== Your code here for business logic==========
    // prepare list for DMLS

}

// ============ DML here ===========

You must log in to answer this question.

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