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;
            //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.



After learning that attachments are depricated, 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];
            // 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;
            //Update the database
            insert newAttObj;

Even now, the attachment of merged record is not getting updated with ParentID of master record

Can anyone please let me know if i missed anything here?

  • Is there a reason you are not using content documents? Attachments I believe are deprecated..
    – gNerb
    Jun 18, 2020 at 15:36
  • @gNerb Updated my code with the given suggestion. Please suggest
    – SK_112
    Jun 18, 2020 at 17:48
  • 2
    If you're using Database.merge method, then all child objects are automatically reparented to the master, so you don't need to do it yourself. Jun 18, 2020 at 18:44
  • @DavidCheng doesn't the merge method only work on accounts leads and contacts?
    – gNerb
    Jun 18, 2020 at 19:38
  • @SK_112 I don't think the provided code is enough for us to troubleshoot, can you include a larger code sample in your question.
    – gNerb
    Jun 18, 2020 at 19:39

1 Answer 1


Thanks @gNerb for suggesting to use content documents instead of attachments as they are depricated in lightning.

Here is my final solution:

   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;
   //Update the database
   insert newAttObj;

I hope this helps someone who is stuck with similar kind of issue

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