Converting attachment into file

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-

  1. From Setup, enter Permission Sets in the Quick Find box, then select Permission Sets.
  2. Create a new permission set.
  3. Select System Permissions.
  4. Click Edit and enable the following permissions:
    1. In the Systems section:
      1. a. Modify All Data
      2. b. Set Audit Fields upon Record Creation
    2. In the Users section:
      1. c. View All Users
  5. Click Save.
  6. Click Manage Assignments.
  7. 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;
    }
}

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply