function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitin LedangeNitin Ledange 

need to avoid for() Inside for() in my Trigger Handler Class code

Hello All,

Can you please help me to optimize below code ?
Need to avoid for() inside for(). Apart from that, if there's any other optimization, you can add that too.
Requirement :  whenever an attachment is inserted/attached to DTM_Uploaded_Document__c record , Clone all the attachment from DTM_Uploaded_Document__c to Merchant_Application Record Or Headless_Merchant_Staging record. There's no relationship between these 3 objects, Only Name is common between them. Also, once all the related attachments has been cloned to related merchant App or Staging, need to set a checkbox field to true of DTM_Uploaded_Document__c parent record.


public class VH_AddAttachemntToMerchantTriggerHandler extends TriggerHandler {
    
    Public override void afterInsert(){

        Map<String, List<Attachment>> NameMap = new Map<String, List<Attachment>>(); 

        List<Attachment> attList ;
        if(!Trigger.New.isEmpty()){
             attList = [SELECT Id, Name, Body, ContentType, Parent.Name, ParentId, Parent.Type FROM Attachment WHERE Id IN: Trigger.New AND Parent.Type = 'DTM_Uploaded_Document__c' LIMIT 50000];   
        }
         
        if(!attList.isEmpty())
        {
            for(Attachment a : attList) {
               if(!NameMap.containsKey(a.Parent.Name)){
                    NameMap.put(a.Parent.Name, new List<Attachment>{a});
                }
                else{
                    NameMap.get(a.Parent.Name).add(a);
                }     
            }  
        }
        

        Set<Id> parentIds = new Set<Id>();
        List<Attachment> insertAttachList = new List<Attachment>();
        Map<String, List<Attachment>> merchAttachMap = new Map<String, List<Attachment>>(); // to check if all the attachment from Document Upload Record has been attached to MerchantApp record OR Stagin Record
        List<VH_Merchant_Application__c    > merchAppList = new List<VH_Merchant_Application__c>([SELECT Id, Name FROM VH_Merchant_Application__c    WHERE Name IN: NameMap.keySet()]);
        List<VH_HeadlessService_MerchantApp_Staging__c> merchStagingList = new List<VH_HeadlessService_MerchantApp_Staging__c>([SELECT Id, Unique_Merchant_Key__c FROM VH_HeadlessService_MerchantApp_Staging__c WHERE Unique_Merchant_Key__c IN: NameMap.keySet()]);
       // clone the attachments to Merchant Application
        if(!merchAppList.isEmpty()){
            for(VH_Merchant_Application__c m : merchAppList){
                if(NameMap.containsKey(m.Name) && !NameMap.get(m.Name).isEmpty()){
                   for(Attachment a : NameMap.get(m.Name)){
                        Attachment b = a.clone();
                        b.ParentId = m.Id;
                        insertAttachList.add(b);
                        parentIds.add(a.ParentId);
                        
                       if(merchAttachMap.containsKey(m.Name)){
                                merchAttachMap.get(m.Name).add(a);   
                       }
                       else{
                           merchAttachMap.put(m.Name, new List<Attachment>{a});
                       }
                    }
                }
        }            
        }
        if(!merchStagingList.isEmpty()){
            for(VH_HeadlessService_MerchantApp_Staging__c vm : merchStagingList){
                if(NameMap.containsKey(vm.Unique_Merchant_Key__c) && !NameMap.get(vm.Unique_Merchant_Key__c).isEmpty()){
                    for(Attachment a : NameMap.get(vm.Unique_Merchant_Key__c)){
                        Attachment b = a.clone();
                        b.ParentId = vm.Id;
                        insertAttachList.add(b);
                        parentIds.add(a.ParentId); 
                        
                        if(merchAttachMap.containsKey(vm.Name)){
                                merchAttachMap.get(vm.Name).add(a);   
                        }
                        else{
                           merchAttachMap.put(vm.Name, new List<Attachment>{a});
                        }
                    }
                }
            }
        }
        if(!insertAttachList.isEmpty()){
              Insert insertAttachList;  
        }
        
        
        List<DTM_Uploaded_Document__c> updateDTMList = new List<DTM_Uploaded_Document__c>();
        if(!parentIds.isEmpty()){
            for(DTM_Uploaded_Document__c d : [SELECT Id, name, Can_be_Deleted__c FROM DTM_Uploaded_Document__c WHERE Id IN: parentIds]){
                if(nameMap.containsKey(d.Name) && merchAttachMap.containsKey(d.Name) && nameMap.get(d.Name).size() == merchAttachMap.get(d.Name).size()){
                    d.Can_be_Deleted__c = true;
                    updateDTMList.add(d);                    
                }

            }
        }
        
        if(!updateDTMList.isEmpty()){
            update updateDTMList;
        }
    }
}