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
sksfdc221sksfdc221 

Parent ID not updating on Attachment

I have a requirment where when 2 records are merged, the attachment related to merged record should be inserted to parent/master record. Below is my code related to this
 
List<Attachment> attObj = [SELECT body, bodylength, contenttype, description, isprivate, name, ownerid FROM Attachment WHERE ParentId = :id2];
            List<Attachment> newAttObj = new List<Attachment>();
            // Loop through the list and update the Name field
            for (Attachment a : attObj) {
                Attachment ax = new Attachment();
                ax.Body = a.Body;
                ax.contenttype = a.contenttype;
                ax.description = a.description;
                ax.isprivate = a.isprivate;
                ax.ParentId = id1;
                ax.name = a.name;
                ax.ownerid = a.ownerid;
                newAttObj.add(ax);
            }
            //Update the database
            insert newAttObj;

here id2 is the merged record and id1 is the master record.

So now, the issue here is that the attachment is not getting inserted to parent/master record after the merge. Even when I hardcode the id as ax.ParentId = '16 digit ID', the record is not getting inserted to parent record.

Now, as per my research, I understood that attachments are depricated in lightning. So as per that, I have updated my code as follows
 
List<ContentDocumentLink> newAttObj = new List<ContentDocumentLink>();
            
            list<ContentDocumentLink> contdoclink = [SELECT Id, LinkedEntityId, ContentDocumentId,Visibility, IsDeleted, ShareType,ContentDocument.Title,ContentDocument.createdDate, ContentDocument.FileType FROM ContentDocumentLink WHERE LinkedEntityId =:id2];
            //list<ContentDocument> contDoc = [Select Id ,Title from ContentDocument Where ID = :contdoclink];
            // Loop through the list and update the Name field
            for (ContentDocumentLink a : contdoclink) {
                ContentDocumentLink ax = new ContentDocumentLink();
                ax.ContentDocumentId = a.ContentDocumentId;
                ax.Visibility = a.Visibility;
                ax.ShareType = a.ShareType;
                ax.ContentDocument.Title = a.ContentDocument.Title;
                ax.LinkedEntityId = id1;
               // ax.ContentDocument.FileType = a.ContentDocument.FileType;
                //ax.ownerid = a.ownerid;
                newAttObj.add(ax);
            }
            //Update the database
            insert newAttObj;

Even now, I attachment is not getting updated to the master record Id.
Can anyone please suggest any changes if i missed anything
Best Answer chosen by sksfdc221
David Zhu 🔥David Zhu 🔥
As you said, Salesforce is not using Attachment object. The new structure of attachment is a bit complicated and a few objects are involved. In your case, you may use ContentVersion to implement this.
 
            List<ContentVersion> attObj = [SELECT Title,PathOnClient,VersionData,FirstPublishLocationId   
                    FROM ContentVersion WHERE FirstPublishLocationId =:id2];
            List<ContentVersion> newAttObj = new List<ContentVersion>();
            // Loop through the list and update the Name field
            for (ContentVersion a : attObj) {
                ContentVersion objCntNote = new ContentVersion();
                objCntNote.Title = a.Title;
                objCntNote.PathOnClient = a.PathOnClient;
                objCntNote.VersionData = a.VersionData;
                objCntNote.FirstPublishLocationId = id1;
                newAttObj.add(objCntNote);
	        }
            //Update the database
            insert newAttObj;