Salesforce has enabled us to utilize files instead of attachment. Many of our customers want to move into lightning from classic experience. one of the most challenging implementation was that hide the attachment option and enable the file upload and all old attachments should be available as files. means that we have to move all attachments into files.
Here is the code to convert your Account records attachments into files. I have used batch class as we have to load millions of attachments.
global without sharing class FilesMigrationBatchClass implements Database.Batchable<SObject>, Database.AllowsCallouts, Database.Stateful {
global Database.QueryLocator start(Database.BatchableContext recordsList){
return Database.getQueryLocator('SELECT Id FROM Account ');
}
global void execute(Database.BatchableContext recordsList, List<Account> scope){
try{
List<Attachment> attachmentRecordsList = new List<Attachment>();
List<ContentVersion> lstContentVersions = new List<ContentVersion>();
Map<Id, List<ContentVersion>> idToContentVersionMap = new Map<Id, List<ContentVersion>>();
Map<Id,Id> mapOfOldIdToNewId = new Map<Id,Id>();
for(Account obj : scope){
mapOfOldIdToNewId.put(obj.id,obj.Id);
}
attachmentRecordsList = [Select Body, BodyLength, ContentType, CreatedById, CreatedDate, Description, Id, LastModifiedById, LastModifiedDate,
Name, OwnerId, Owner.IsActive, ParentId FROM Attachment WHERE ParentId IN: mapOfOldIdToNewId.keyset()];
Map<Id, Id> contentVersionIdToParentIdMap = new Map<Id, Id>();
for(Attachment attach : attachmentRecordsList) {
ContentVersion cVersion = new ContentVersion();
if(!idToContentVersionMap.containsKey(mapOfOldIdToNewId.get(attach.ParentId))){
idToContentVersionMap.put(mapOfOldIdToNewId.get(attach.ParentId), new List<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 = 'C';//C-Content Origin. H-Chatter Origin.
cVersion.Title = attach.Name;//Name of the file
cVersion.VersionData = attach.Body;//File content
cVersion.Description = attach.Description;//File Descriptions or comments
cVersion.Attachment_Id__c = attach.Id;//create a new field on content version to store old attachment id
idToContentVersionMap.get(mapOfOldIdToNewId.get(attach.ParentId)).add(cVersion);
}
if(!idToContentVersionMap.isEmpty()) {
for(List<contentVersion> contentVersionRec : idToContentVersionMap.values()){
lstContentVersions.addAll(contentVersionRec);
}
insert lstContentVersions;
}
for(Id conVer : idToContentVersionMap.keyset()){
for(ContentVersion obj : idToContentVersionMap.get(conVer)){
contentVersionIdToParentIdMap.put(obj.Id,conVer);
}
}
List<ContentVersion> conList = new List<ContentVersion>();
conList = [Select Id, contentDocumentId FROM ContentVersion WHERE id =: contentVersionIdToParentIdMap.keyset()];
Map<Id, Id> docIdwithparentId = new Map<Id,Id>();
for(ContentVersion conVerObj : conList){
docIdwithparentId.put(conVerObj.contentDocumentId,contentVersionIdToParentIdMap.get(conVerObj.Id));
}
List<ContentDocumentLink> conDocLinkList = new List<ContentDocumentLink>();
for(Id docId : docIdwithparentId.keyset()){
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = docId;
cdl.LinkedEntityId = docIdwithparentId.get(docId);
cdl.ShareType = 'I';
conDocLinkList.add(cdl);
}
if(!conDocLinkList.isEmpty()){
insert conDocLinkList;
}
}catch(Exception Exp){
system.debug('Execute Method.....', Exp);
}
}
global void finish(Database.BatchableContext recordsList){
}
}
Comments
Post a Comment