One thing to realize is that
ContentDocument can't be directly inserted; it is inserted automatically when the first ContentVersion is inserted. ContentDocument acts as a parent for each version of the file - i.e., each child
So an approach would be:
- Export all ContentVersion from the source (including the ID)
- Insert the ContentVersion to the target org. Associate in some place (Excel), the target ContentVersion.Id to the source org's ContentVersion.Id
- Export and insert all Sobjects (like Account, Opportunity, etc - i.e., those Sobjects that are the parents of ContentDocumentLink as
LinkedEntityId. Be sure to keep track of the new (target) sobject Ids and associate them (Excel) back to the source Sobject Ids.
- Export the ContentDocumentLink from the source org. This is tricky because you can't SOQL all of the ContentDocumentLink without a Where clause. If you don't have too many ContentDocument, you can do this in one SOQL but if you have way too many (exceed size of SOQL string), you will need to write a small batchable class to export the ContentDocumentLinks to a custom object that you can in turn export using DataLoader.
- Manipulate the exported ContentDocumentLink from the source org (Excel) to replace the source LinkedEntityId with the ID of that Sobject in the target; do the same for the ContentDocumentId.
- Use DataLoader to insert the ContentDocumentLink into the target
Details on the batchable:
start() - query all ContentDocument
execute() - query all
ContentDocumentLink for the execute's scope argument (i.e. a list of
ContentDocument. Write results to a custom migration object -
ContentDocumentLink_Migration__c that has two fields: