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
Phuc Nguyen 18Phuc Nguyen 18 

Iterating over same list twice

        
 
I think I am iterating ov erht same list twice.  Will someone help me streamline the code.  Can I get the second for loop inside the first and be efficient?
Attachment__c newAttachment = (Attachment__c ) so;
        String objectType;
        List<Plan__c > palnId = new List<Plan__c>();
        Map<String,String> planMap = new Map<String,String>();
        
        if( this.linkMap.containsKey( newAttachment.ContentDocumentId__c ) ){
            List<ContentDocumentLink> links = this.linkMap.get( newAttachment.ContentDocumentId__c );
            if( !links.isEmpty() ){
                for( ContentDocumentLink link : links ){
                    objectType = String.valueOf( link.LinkedEntityId.getSObjectType() );
                    switch on ( objectType ){
                        when 'Activity__c' {
                            newAttachment.Activity__c= link.LinkedEntityId;
                        }
                        when 'Project__c' {
                            newAttachment.Proj__c= link.LinkedEntityId;
                        }
                    }
                }
            }
    
            List<Plan__c> plans= new List<Plan__c>();

            for (ContentDocumentLink cont : links ){                    
                if(newAttachment.Activity__c != null || newAttachment.Proj__c != null){
                    ContentDocumentLink cdl = new ContentDocumentLink();
                    cdl.LinkedEntityId = parentId;
                    cdl.ContentDocumentId = cont.ContentDocumentId;
                    cdl.ShareType = 'V';
                    cdl.Visibility = 'AllUsers';
                    cdl_List.add(cdl);
                    break;
                }
            }
        }

 
Best Answer chosen by Phuc Nguyen 18
Andrew GAndrew G
Create a method to handle the creation of the CDL
this part:
ContentDocumentLink cdl = new ContentDocumentLink();
                    cdl.LinkedEntityId = parentId;
                    cdl.ContentDocumentId = cont.ContentDocumentId;
                    cdl.ShareType = 'V';
                    cdl.Visibility = 'AllUsers';
                    cdl_List.add(cdl);
I would have the method accept the List of CDL and the whatever parameters are required to create the record.  From the snippet, it's hard to be clear by probably
public static List<CDL> createNewCDL(list<CDL>,ParentId, ContentDocID) {
// some code
}

then in your first loop
                       when 'Activity__c' {
                            newAttachment.Activity__c= link.LinkedEntityId;
                            //invoke the createNewCDL method
                           someClass.createNewCDL(cdl_List,parentId,cont.ContentDocumentId);
                        }
                        when 'Project__c' {
                            newAttachment.Proj__c= link.LinkedEntityId;
                            //invoke the createNewCDL method
                            someClass.createNewCDL(cdl_List,parentId,cont.ContentDocumentId); 
                        }
I think that should point you in the right direction

regards 
Andrew