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
Tyler Harris 8Tyler Harris 8 

Trying to clone attachment and associate to new object record

Hello,

I'm trying to query attachments associated to a 'published' version of a record and clone the record and the attachment. So far I've been able to clone the record, but I can't get the attachment to relate to the new record. I'm doing this in a class that I'm using in a trigger.
 
public void updateDraftRecord(Map<Id,Partner_Locator_Detail__c> newMap){
        List<Partner_Locator_Detail__c> ptr = [SELECT Id, Name, Partner_Account__c,Publication_Status__c FROM Partner_Locator_Detail__c WHERE Publication_Status__c = 'Published' ];
	 
        Map<Id, Partner_Locator_Detail__c> acctPldMap = new Map<Id, Partner_Locator_Detail__c>();
         for(Partner_Locator_Detail__c pld:ptr){
            acctPldMap.put(pld.Partner_Account__c, pld);
        }
         system.debug(acctPldMap);
        
       // List<Attachment> atc = [SELECT Id, Name, ParentID FROM Attachment WHERE ParentID IN :acctPldMap.values()];
        
       // Map<Id, Attachment> attachMap = new Map<Id, Attachment>();
     
        List<Business_Segment__c> biz = [SELECT Id, Advantage_Partner_Program_Tier__c,Applications_Usage__c,Business_Segment__c,Field_Embedded_Images__c,Field_Product_Image__c,Field_Product_Image2__c,Field_Product_Image2_Timestamp__c,	Field_Product_Image_Timestamp__c,Industries__c,Partner_Type__c,	Product_Portfolio_Alternate_Text__c,Product_Portfolio_Title__c,Products__c,	Regions__c,	Segment_Overview__c,Segment_Headline__c, Partner_Locator_Detail__c FROM Business_Segment__c WHERE Partner_Locator_Detail__c IN :acctPldMap.values() ];
          Map<Id, Business_Segment__c> ptrBizMap = new Map<Id, Business_Segment__c>();
          system.debug(ptrBizMap);
         
         for(Business_Segment__c bizSeg: biz){
            ptrBizMap.put(bizSeg.Partner_Locator_Detail__c, bizSeg);
        }
         system.debug(ptrBizMap);
        List<Attachment> bizAttach = [SELECT Id,Body,BodyLength,ContentType,Description, Name, ParentID FROM Attachment WHERE ParentID IN :ptrBizMap.values()];
         system.debug(bizAttach);
         Map<Id, Attachment> bizAttachMap = new Map<Id, Attachment>();
         for(Attachment attach: bizAttach){
             bizAttachMap.put(attach.ParentId, attach);
         }
         system.debug(bizAttachMap.values());
		List<Business_Segment__c> bizSegInsert = new List<Business_Segment__c>();
		List<Attachment> attachmentToInsert = new List<Attachment>();
        for(Partner_Locator_Detail__c psd : newMap.values()){
            if(acctPldMap.containsKey(psd.Partner_Account__c) && psd.Publication_Status__c=='Draft' && psd.isClone()){
                if(ptrBizMap.size() >0 && ptrBizMap.containsKey(acctPldMap.get(psd.Partner_Account__c).id)){
                    Business_Segment__c bizs = ptrBizMap.get(acctPldMap.get(psd.Partner_Account__c).id);
                    Business_Segment__c bizClone = bizs.clone(false,true);
                   
                    bizClone.Partner_Locator_Detail__c = psd.id;
                    BizSegInsert.add(bizClone);
                    if(psd.Partner_Account__c !=null){
                        if(ptrBizMap.get(psd.Partner_Account__c) != null ){
                     if(bizAttachMap.size() > 0 && bizAttachMap.containsKey(ptrBizMap.get(psd.Partner_Account__c).Id)){
                    	Attachment biza = bizAttachMap.get(ptrBizMap.get(psd.Partner_Account__c).Id);
                         system.debug(biza);
                        Attachment bizaClone = biza.clone(false,true);
                        bizaClone.ParentId = psd.id;
                        attachmentToInsert.add(bizaClone);
                	} 
                        }
                    }
                }
                
            }
            
        }
         if(BizSegInsert !=null && BizSegInsert.size()>0){
             insert BizSegInsert;
         }
         if(attachmentToInsert !=null && attachmentToInsert.size() > 0){
             insert attachmentToInsert;
         }
        
    }

It starts to fall apart when I try to generate a map of the attachment ids here:
List<Attachment> bizAttach = [SELECT Id,Body,BodyLength,ContentType,Description, Name, ParentID FROM Attachment WHERE ParentID IN :ptrBizMap.values()];
         system.debug(bizAttach);
         Map<Id, Attachment> bizAttachMap = new Map<Id, Attachment>();
         for(Attachment attach: bizAttach){
             bizAttachMap.put(attach.ParentId, attach);
         }



Debug log:
10:23:33.0 (1097819166)|METHOD_ENTRY|[17]|01p34000000tOfg|GlobalPRM_PartnerLocatorTriggerHelper.updateDraftRecord(Map<Id,Partner_Locator_Detail__c>) 10:23:33.0 (1107909785)|SOQL_EXECUTE_BEGIN|[36]|Aggregations:0|SELECT Id, Name, Partner_Account__c, Publication_Status__c FROM Partner_Locator_Detail__c WHERE Publication_Status__c = 'Published' 10:23:33.0 (1116469314)|SOQL_EXECUTE_END|[36]|Rows:230 10:23:33.0 (1145927691)|SOQL_EXECUTE_BEGIN|[48]|Aggregations:0|SELECT Id, Advantage_Partner_Program_Tier__c, Applications_Usage__c, Business_Segment__c, Field_Embedded_Images__c, Field_Product_Image__c, Field_Product_Image2__c, Field_Product_Image2_Timestamp__c, Field_Product_Image_Timestamp__c, Industries__c, Partner_Type__c, Product_Portfolio_Alternate_Text__c, Product_Portfolio_Title__c, Products__c, Regions__c, Segment_Overview__c, Segment_Headline__c, Partner_Locator_Detail__c FROM Business_Segment__c WHERE Partner_Locator_Detail__c IN :tmpVar1 10:23:33.0 (1333273631)|SOQL_EXECUTE_END|[48]|Rows:410 10:23:33.0 (1385104076)|SOQL_EXECUTE_BEGIN|[56]|Aggregations:0|SELECT Id, Body, BodyLength, ContentType, Description, Name, ParentID FROM Attachment WHERE ParentID IN :tmpVar1 10:23:34.102 (2102263073)|SOQL_EXECUTE_END|[56]|Rows:17 10:23:34.102 (2108363986)|DML_BEGIN|[90]|Op:Insert|Type:Business_Segment__c|Rows:1 10:23:34.102 (2139209464)|DML_END|[90]