You need to sign in to do that
Don't have an account?
sksfdc221
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
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
Even now, I attachment is not getting updated to the master record Id.
Can anyone please suggest any changes if i missed anything
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 🔥
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.