When you convert attachment to File, following must be kept in mind-
1. Filename should be without extension. It can be with extension but its better to without extension.
2. CreatedDate/LastModifiedDate of the file must be same that of the attachment. Create a permission set for that. Follow the steps to create the same-
public without sharing class ConvertAttachment {
public static void convertAttachmentIntoFile(Id attachmentId) {
//Get attachment
Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId, LastModifiedDate, Description From Attachment where id =: attachmentId LIMIT 1];
//Insert ContentVersion
ContentVersion cVersion = new ContentVersion();
cVersion.ContentLocation = ‘S’; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
cVersion.PathOnClient = attach.Name; //File name with extention
cVersion.Origin = ‘H’;//C-Content Origin. H-Chatter Origin.
cVersion.OwnerId = attach.OwnerId; //Owner of the file
cVersion.Title = attach.Name.subString(0, attach.Name.indexOf(‘.’)); //File name without extention
cVersion.VersionData = attach.Body;//File content
cVersion.CreatedById = attach.OwnerId;
cVersion.LastModifiedDate = attach.LastModifiedDate;
cVersion.createdDate = attach.LastModifiedDate;
if(attach.Description != null) {
cVersion.Description = attach.Description;
}
Insert cVersion;
//After saved the Content Verison, get the ContentDocumentId
Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;
//Insert ContentDocumentLink
ContentDocumentLink cDocLink = new ContentDocumentLink();
cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId
cDocLink.LinkedEntityId = attach.ParentId;//Add attachment parentId
cDocLink.ShareType = ‘I’;//V – Viewer permission. C – Collaborator permission. I – Inferred permission.
cDocLink.Visibility = ‘InternalUsers’;//AllUsers, InternalUsers, SharedUsers
Insert cDocLink;
}
}
1. Filename should be without extension. It can be with extension but its better to without extension.
2. CreatedDate/LastModifiedDate of the file must be same that of the attachment. Create a permission set for that. Follow the steps to create the same-
- From Setup, enter Permission Sets in the Quick Find box, then select Permission Sets.
- Create a new permission set.
- Select System Permissions.
- Click Edit and enable the following permissions:
- In the Systems section:
- a. Modify All Data
- b. Set Audit Fields upon Record Creation
- In the Users section:
- c. View All Users
- In the Systems section:
- Click Save.
- Click Manage Assignments.
- Assign the permission set to anyone who will be converting attachments into files. This typically includes admins.
Please find the code for the conversion(Its for specific attachment migration but Adjust the code accordingly with savepoint if you are converting attachments in bulk. You may want to delete the attachment at the same times.). Everything is self-explanatary. Leave a comment if want to know more or migrating your org from classic to lightning and need help on notes/attachment migration.-
public without sharing class ConvertAttachment {
public static void convertAttachmentIntoFile(Id attachmentId) {
//Get attachment
Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId, LastModifiedDate, Description From Attachment where id =: attachmentId LIMIT 1];
//Insert ContentVersion
ContentVersion cVersion = new ContentVersion();
cVersion.ContentLocation = ‘S’; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
cVersion.PathOnClient = attach.Name; //File name with extention
cVersion.Origin = ‘H’;//C-Content Origin. H-Chatter Origin.
cVersion.OwnerId = attach.OwnerId; //Owner of the file
cVersion.Title = attach.Name.subString(0, attach.Name.indexOf(‘.’)); //File name without extention
cVersion.VersionData = attach.Body;//File content
cVersion.CreatedById = attach.OwnerId;
cVersion.LastModifiedDate = attach.LastModifiedDate;
cVersion.createdDate = attach.LastModifiedDate;
if(attach.Description != null) {
cVersion.Description = attach.Description;
}
Insert cVersion;
//After saved the Content Verison, get the ContentDocumentId
Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;
//Insert ContentDocumentLink
ContentDocumentLink cDocLink = new ContentDocumentLink();
cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId
cDocLink.LinkedEntityId = attach.ParentId;//Add attachment parentId
cDocLink.ShareType = ‘I’;//V – Viewer permission. C – Collaborator permission. I – Inferred permission.
cDocLink.Visibility = ‘InternalUsers’;//AllUsers, InternalUsers, SharedUsers
Insert cDocLink;
}
}

