We would like to move attachments for some Accounts to a different custom object. However since parentid is a readonly field , i cannot change the parentid and reassociate the attachments. So I was thinking of exporting attachments from Salesforce and then reimporting into Salesforce and link to the custom object which want them to be related to.

 I want to be able to export attachments based on soql queries and then later import them into a different object.   So far I have looked at dataloader / weekly export / dataloader.io ( 3rd party tool) / fileexport( 3rd party tool). 

1) Salesforce Weekly export : Since this is an entire dump of all attachments. So I am guessing it will not work in my case .

2) dataloader.io : This is a 3rd party utility suggested by the developer support rep. The soql is working fine for 1 attachment. However if I write a soql which will retrieve multiple attachments, it does not work .

3) fileexport : It is a 3rd party tool, which I could not even get to install properly. I stopped working on this after burning a couple of hours.

4) Dataloader : I am able to export the attachment record as a .csv file but get a parse error when I try to use it to import.

Any suggestions on how to proceed would be very much appreciated.

Thanks, Vasu

1 Answer 1


This can be done fairly simply within Salesforce using Apex code avoiding exports and imports.

You query all the Attachment fields (Name, Body, ParentId, OwnerId etc) and then create new Attachment objects for each one assigning each old field to the new object, but of course changing the ParentId to the new parent id. Once the new objects are inserted you can then go and delete the old ones. This sequence avoids the "Field not writable" error.

(We have some code that uses SObject.clone and then overwrites the ParentId field so it looks like the "Field not writable" constraint is only imposed on persisted Attachment objects and before the objects are persisted the value can be changed.)

If the number and size of the Attachments (the Body) is large so that governor limits are an issue, then this can be wrapped as a Batchable where as few as one Attachments per batch are processed.

You must log in to answer this question.

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