function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mahmood ButtMahmood Butt 

Migrate attachments to Salesforce Files

One of our clients has a requirement to migrate attachments to Salesforce Files. Salesforce Files (ContentDocument) object doesn't support insert().
They have also enabled this option from Winter '16 to allow file uploads to goto salesforce files instead of attachments.
User-added image

I have confirmed from Salesforce docs and data loader pertaining ContentDocument's supported calls. Now, is there a way I can still pull this task off of migrating attachments to salesforce files?
Best Answer chosen by Mahmood Butt
Mahmood ButtMahmood Butt
After a lot RnD, I managed to migrate attachments to Salesforce Files using the following steps. 
  1. Export Attachments data using data loader. 
  2. Export Attachments files, using data export wizard or File exporter. 
  3. Create two fields, ParentId and AttachmentId on ContentVersion object 
    • ParentId will hold the id of the entity, attachment was linked to. 
    • AttachmentID (with unique constraint) will hold the id of the attachment from attachment object.
  4. Create a CSV, ContentVersion_upload.csv, for ContentVersion with the following fields
    • Attachment_id (AttachmentId from file exported in step-1)
    • ISDELETED 
    • PARENTID (Entity Id of the object, from file exported in step 1) 
    • Versiondata (local file path) 
    • PathOnClient (local file path) 
    • Title (title of the file) 
    • OWNERID 
    • DESCRIPTION
  5. Upload (Insert) ContentVersion.csv into ContentVersion object. 
  6. Export ContentVersion data uploaded in step-5 using data loader. 
  7. Create another CSV for ContentDocumentLink (and update ContentDocumentID andParentEntityId, from the ContentVersion file exported in step-5) with the following fields: 
    • CONTENTDOCUMENTID (ContentDocumentId from file exported in step-6) 
    • LINKEDENTITYID (ParentId from file exported in step-6) 
    • SHARETYPE 
    • VISIBILITY
  8. Upload (Insert) ContentDocumentLink.

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Butt,
You can specifiy the path in the body field while inserting attachment through data loader.
 
Mahmood ButtMahmood Butt
After a lot RnD, I managed to migrate attachments to Salesforce Files using the following steps. 
  1. Export Attachments data using data loader. 
  2. Export Attachments files, using data export wizard or File exporter. 
  3. Create two fields, ParentId and AttachmentId on ContentVersion object 
    • ParentId will hold the id of the entity, attachment was linked to. 
    • AttachmentID (with unique constraint) will hold the id of the attachment from attachment object.
  4. Create a CSV, ContentVersion_upload.csv, for ContentVersion with the following fields
    • Attachment_id (AttachmentId from file exported in step-1)
    • ISDELETED 
    • PARENTID (Entity Id of the object, from file exported in step 1) 
    • Versiondata (local file path) 
    • PathOnClient (local file path) 
    • Title (title of the file) 
    • OWNERID 
    • DESCRIPTION
  5. Upload (Insert) ContentVersion.csv into ContentVersion object. 
  6. Export ContentVersion data uploaded in step-5 using data loader. 
  7. Create another CSV for ContentDocumentLink (and update ContentDocumentID andParentEntityId, from the ContentVersion file exported in step-5) with the following fields: 
    • CONTENTDOCUMENTID (ContentDocumentId from file exported in step-6) 
    • LINKEDENTITYID (ParentId from file exported in step-6) 
    • SHARETYPE 
    • VISIBILITY
  8. Upload (Insert) ContentDocumentLink.
This was selected as the best answer
april santiago 10april santiago 10
Hi @mahmood, 

Please advise if there is any way to export ContentDocumentLink from the customer's Group Editions Org. 
Snehil KarnSnehil Karn
Hi All,

I used the following code to convert Case Attchment to Case chatter file (Content Version):
 
Attachment at = [Select Body, Id, Name, OwnerId,ParentId From Attachment LIMIT 1];

ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.PathOnClient = at.Name;
cv.Origin = 'H';
cv.OwnerId = at.OwnerId;
cv.Title = at.Name;
cv.VersionData = at.Body;
insert cv;


ContentVersion cv = [select ContentDocumentId from ContentVersion where Id =: cv.Id];

ContentDocumentLink cl = new ContentDocumentLink(LinkedEntityId = at.ParentId, ContentDocumentId = cv.ContentDocumentId, ShareType = 'I');
insert cl;

This sample code convers an Attachment record to a Chatter File. You can later delete the attachment if you do not need it.
Leora Ferster 5Leora Ferster 5
@Snehil Karn is this a trigger on case?