9

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?

  • I know this is an old post, but is the only way to move an attachment to copy it by inserting and delete from other record? The comment on here about upsert to external id, is that to move it or is that still copy and delete? – john May 11 '17 at 16:40
  • Hi @john, The upsert is for Copy and Delete only process. It is just for referencing the parent records. – Chirag Verma May 29 '17 at 9:34
10

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;
  • 2
    welcome to salesforce.stackoverflow ! Can you try to format all the code you posted ? edit and use the {} button in the editor. – Samuel De Rycke Jan 10 '14 at 12:33
7

ParentId is not updatable on Attachment, as you've discovered. This means you will not be able to modify the existing attachments to reparent them to a new parent.

You will probably need to use an external ID on the parent records when you load the attachments in the first place.

This involved setting either a text, number, or email field to both ExternalId and Unique.

Let's say you do this with Account and you add the External Id field called Import_Key__c. Once you've added that field you will need to populate it with a unique value. This can be anything, even a duplicate of the record Id.

Then your mapping will look something like this:

SourceField          Salesforce Field
---------------------------------------------------------------------
ParentId             Parent.Import_Key__c

You need to then use the Upsert operation. When upserting the attachment, salesforce will use the external Id field value to map back to the proper parent record Id and populate ParentId with the correct value.

Yes, you will need to reload all 6GB of those attachments, but I'm afraid it is the only way.

  • Actually there are field in my Objects i.e. Account, Contact, Opportunity etc that can used as externalID. Can I able to map them now in the UPSERT operation of Attachment with those records ExternalID(uniques) ? – Chirag Verma Sep 2 '13 at 9:26
  • Is there not any way, through which I raise a case in Premium Support and they will enable the 'parentID' field in editable mode during Update Operation for Attachments ? @Peter – Chirag Verma Sep 2 '13 at 9:29
  • They might work. Give them a try. I would also contact support. I'm not aware that this is something they can provision for temporarily or help you perform, but you can only find out if you ask. – pchittum Sep 2 '13 at 23:15
  • 1
    Honestly, in most cases, it would be faster to simply delete the records and re-insert them. I've had to do this before, and it's not as painful as it sounds, unless you have dialup or DSL. – sfdcfox Sep 3 '13 at 5:08
  • yeah, I am doing the same presently. – Chirag Verma Sep 4 '13 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.