[SalesForce] How to update parentID on Attachments in Salesforce

I need to move Attachments from one SF to another SF, but the final parent object is not in the destination org. So I've created temporary records and plan to update the ParentId of the attachments later.

But when I was ready to update the ParentId of the Attachments, it didn't show a map to the "parentID". I read somewhere that when we insert Attachments its parentID becomes read-only after insertion. I loaded around 6GB of data to my temporary records. How can I modify the ParentIds of all those Attachments?

Best Answer

You can clone the entire attachment and then delete the old one.

this code works for me :

List<Attachment> attachmentsToInsert = new List<Attachment>();

List<Attachment> attachmentsToDelete = new List<Attachment>();

Attachment tempAtt;


for(Attachment attachment: [
        SELECT SystemModstamp, ParentId, OwnerId, Name, 
            LastModifiedDate, LastModifiedById, IsPrivate, IsDeleted, 
            Id, Description, CreatedDate, CreatedById, 
            ContentType, BodyLength, Body 
        FROM Attachment 
        WHERE parentId=:'parentid']) {

    tempAtt=attachment.clone(false,false);
    tempAtt.parentId = quoteSelWrap1.quot.Id;

    attachmentstoInsert.add(tempAtt);
    attachmentsToDelete.add(attachment);

}

insert attachmentsToInsert;
delete attachmentsToDelete;
Related Topic