global without sharing class Batch_FixnoteToFiles implements Database.Batchable { // Id batchJobId = Database.executeBatch(new Batch_FixAttachmentToFiles('Consumable_order__c',Datetime.newInstance(2023, 1, 1, 8, 0, 0),Datetime.newInstance(2024, 1, 1, 8, 0, 0)),2000); private Datetime creStartDate = null; private Datetime creEndDate = null; private String objectApiName = null; private Set parentIds = new Set(); global Batch_FixnoteToFiles(String objectType, Datetime startTime, Datetime endTime) { creStartDate = startTime; creEndDate = endTime; objectApiName = objectType; } public Batch_FixnoteToFiles(Set parentIds) { this.parentIds = parentIds; } public Batch_FixnoteToFiles() { } global Database.QueryLocator start(Database.BatchableContext bc) { String queryObject = 'SELECT Id, title, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM note'; if (String.isNotBlank(objectApiName)) { queryObject = 'SELECT Id, title, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM note WHERE Parent.Type =:objectApiName AND CreatedDate >=:creStartDate AND CreatedDate <:creEndDate Order by CreatedDate ASC'; } if(parentIds.size() > 0){ queryObject = 'SELECT Id, title, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM note WHERE ParentId in:parentIds'; } return Database.getQueryLocator(queryObject); } global void execute(Database.BatchableContext BC, List scope) { List insertContents = new List(); Map nameParentMaps = new Map(); Set noteIds = new Set(); for (note att : scope) { nameParentMaps.put(att.Id, att.ParentId); noteIds.add(att.Id); 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 = att.Title + '.snote'; //File name with extention //cVersion.Origin = 'H'; //C-Content Origin. H-Chatter Origin. cVersion.Title = att.Title; //Name of the file cVersion.FirstPublishLocationId = att.ParentId; String body = String.valueOf(att.Body); if (String.isNotBlank(body)) { cVersion.VersionData = Blob.valueOf(Body); } //cVersion.VersionData = Blob.valueOf(att.Body); insertContents.add(cVersion); } if(insertContents.isEmpty()){ return; } //Insert insertContents; List saveResultsContents = Database.insert(insertContents, false); insertLog(saveResultsContents,noteIds); /*set contentIds = new set(); for(ContentVersion cv : insertContents){ contentIds.add(cv.id); } List conDocuments = [SELECT ContentDocumentId, Title,PathOnClient FROM ContentVersion WHERE Id in: contentIds]; List insertDocLinks = new List(); for(ContentVersion cv : conDocuments){ ContentDocumentLink cDocLink = new ContentDocumentLink(); cDocLink.ContentDocumentId = cv.ContentDocumentId; //Add ContentDocumentId cDocLink.LinkedEntityId = nameParentMaps.get(cv.PathOnClient); //Add attachment parentId cDocLink.ShareType = 'V'; //V - Viewer permission. C - Collaborator permission. I - Inferred permission. cDocLink.Visibility = 'AllUsers'; insertDocLinks.add(cDocLink); } //Insert insertDocLinks; List saveResultsLinks = Database.insert(insertDocLinks, false); insertLog(saveResultsLinks,noteIds);*/ } global void insertLog(List saveResults,Set noteIds) { Transaction_Log__c traLog = new Transaction_Log__c(); List> logMapList = new List>(); for (Database.SaveResult result : saveResults) { String recordId = result.getId(); if (!result.isSuccess()) { for (Database.Error error : result.getErrors()) { Map logMap = new Map(); String errorMsg = error.getMessage(); logMap.put('recordId',recordId); logMap.put('errorMsg',errorMsg); logMapList.add(logMap); } } } if(logMapList.size() > 0){ Map attachmentIdMap = new Map(); attachmentIdMap.put('noteIds',JSON.serialize(noteIds)); logMapList.add(attachmentIdMap); traLog.Response__c = JSON.serialize(logMapList); traLog.Module__c = 'note COnvert Transaction '; Insert traLog; } } global void finish(Database.BatchableContext BC) { } }