How to convert attachments to files in salesforce ?

Poster 2 basic


I have uploaded an attachment (salesfiorce_migration_guide.pdf) to an opportunity record from classic salesforce. The uploaded document record has the following actions where ‘view’ action is a link to download the file.

Screen Shot 1940-02-11 at 5.05.59 PM
Uploaded attachments to an opportunity record

Next, uploading another file (router-sample.pdf) to the same opportunity record using upload file functionality in lightning. The uploaded files in the above and current scenario are shown below.

Screen Shot 1940-02-11 at 5.22.26 PM
Uploaded Files & attachment links

You may have noticed that both the files are in PDF format but the recently uploaded file has the pdf icon which we can preview the content without downloading. The other file which is uploaded as an attachment from classic salesforce is treated as a link here.

Therefore, how we can convert the attachments from classic view to files in Salesforce?


It’s possible to change the attachments to files by enabling a couple of checkboxes in GeneralSettings.

Screen Shot 1940-02-11 at 6.31.33 PM

Why Files?

You can attach a file to the notes & attachments related list of record. The users who have access to the record can see the attached documents. The only way to share the record documents with others is by changing the org’s record sharing rules or posting documents in the chatter feed. Salesforce was trying to solve this in a better way.

Finally, In winter 16 release, Salesforce announced a new related list called files. The benefits of files are

  • Version Control
  • Preview & Options
    • Screen Shot 1940-02-11 at 7.03.27 PM
  • Share with multiple users and records
  • Optimised for lightning
  • Post to chatter 
  • Supports large size


The attachments are uploaded in the past cannot be converted as files by enabling the above settings. Run the script given below (script should run by the record owner otherwise it will throw errors). 

//Fetching all available attachments in the org
List<Attachment> attachments = [Select Body, Id, Name, OwnerId,ParentId From Attachment];
//Details of content version fields are given in the below link
//to map Attachment Id with ContentVersion record
Map<Id,ContentVersion> attachmentCVs = new Map<Id,ContentVersion>();
//Map —> Attachement – Attachment Parent Id
Map<Id,Id> attachementParentIds = new Map<Id,Id>();
//Generating file version using content version object
for(Attachment att : attachments) {
ContentVersion cv = new ContentVersion();
//S – Salesforce , check details on above link
cv.ContentLocation = 'S';
cv.PathOnClient = att.Name;
//H – Chatter , check details on above link
cv.Origin = 'H';
cv.OwnerId = att.OwnerId;
cv.Title = att.Name;
cv.VersionData = att.Body;
//Insert the content versions from attachment data
if(attachmentCVs.values().size() > 0 ) {
insert attachmentCVs.values();
//to map ContentVersionId with AttachmentParent Id for ContentDocument Linking
map<Id,Id> cvToAttchmtParentIdMap = new map<Id,Id>();
List<Id> cvIds = new List<Id>();
for(Id key : attachmentCVs.keySet()){
ContentVersion cv = attachmentCVs.get(key);
Id attchmentParentId = attachementParentIds.get(key);
//Fetching contentDocumentId using contentVersionId
List<ContentVersion> cvWithDocIds=[select Id,ContentDocumentId from ContentVersion where Id IN :cvIds];
//to link the files (versions) to its parent records like opportunity,case etc.
List<ContentDocumentLink> contentDocumentLinks = new List<ContentDocumentLink>();
for (ContentVersion cv : cvWithDocIds) {
Id attachmentParentId = cvToAttchmtParentIdMap.get(cv.Id);
ContentDocumentLink cl = new ContentDocumentLink(LinkedEntityId = attachmentParentId, ContentDocumentId = cv.ContentDocumentId, ShareType = 'I');
if(contentDocumentLinks.size() > 0){
insert contentDocumentLinks;
//Delete all exisiting attachments once it is converted into Files if needed.
if(attachments.size() > 0) {
delete attachments;

Please see the code comments for details.

Note: Salesforce announced that after Winter 18 release, ‘Notes & Attachments’ related list will not have “attach and upload” button.

Poster 2 basic

Don’t forget to share the post if you like it & bookmark the blog for future references.

If you have any comments or doubts on this post, Please comment on the box below.



1 thought on “How to convert attachments to files in salesforce ?”

Leave a Reply to Srinivasan P R Cancel reply

%d bloggers like this: