<<<<<<< HEAD global without sharing class Batch_FixAttachmentToFiles 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(); //Add by Li Jun 20230703 ======= global without sharing class Batch_FixAttachmentToFiles 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();//Add by Li Jun 20230703 >>>>>>> LEXCommunityLiJun public Batch_FixAttachmentToFiles(String objectType, Datetime startTime, Datetime endTime) { creStartDate = startTime; creEndDate = endTime; objectApiName = objectType; } //Add by Li Jun 20230703 Start public Batch_FixAttachmentToFiles(Set parentIds) { this.parentIds = parentIds; } <<<<<<< HEAD //Add by Li Jun 20230703 End global Database.QueryLocator start(Database.BatchableContext BC) { String queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE Parent.Type =:objectApiName AND CreatedDate >=:creStartDate AND CreatedDate <:creEndDate Order by CreatedDate ASC'; if (parentIds.size() > 0) { ======= //Add by Li Jun 20230703 End global Database.QueryLocator start(Database.BatchableContext BC) { String queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE Parent.Type =:objectApiName AND CreatedDate >=:creStartDate AND CreatedDate <:creEndDate Order by CreatedDate ASC'; if(parentIds.size() > 0){ >>>>>>> LEXCommunityLiJun queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE ParentId in:parentIds'; } return Database.getQueryLocator(queryObject); } global void execute(Database.BatchableContext BC, List scope) { List insertContents = new List(); <<<<<<< HEAD Map nameParentMaps = new Map(); Set attachmentIds = new Set(); for (Attachment att : scope) { nameParentMaps.put(att.Name, att.ParentId); attachmentIds.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.Name; //File name with extention cVersion.Origin = 'C'; //C-Content Origin. H-Chatter Origin. cVersion.Title = att.Name; //Name of the file cVersion.VersionData = att.Body; //File content insertContents.add(cVersion); } if (insertContents.isEmpty()) { ======= Map nameParentMaps = new Map(); Set attachmentIds = new Set(); for (Attachment att : scope) { nameParentMaps.put(att.Id, att.ParentId); attachmentIds.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.Id; //File name with extention cVersion.Origin = 'C'; //C-Content Origin. H-Chatter Origin. cVersion.Title = att.Name; //Name of the file cVersion.VersionData = att.Body; //File content insertContents.add(cVersion); } if(insertContents.isEmpty()){ >>>>>>> LEXCommunityLiJun return; } //Insert insertContents; List saveResultsContents = Database.insert(insertContents, false); <<<<<<< HEAD insertLog(saveResultsContents, attachmentIds); set contentIds = new Set(); for (ContentVersion cv : insertContents) { contentIds.add(cv.id); } List conDocuments = [ SELECT ContentDocumentId, Title 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.Title); //Add attachment parentId cDocLink.ShareType = 'V'; //V - Viewer permission. C - Collaborator permission. I - Inferred permission. ======= insertLog(saveResultsContents,attachmentIds); 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. >>>>>>> LEXCommunityLiJun cDocLink.Visibility = 'AllUsers'; insertDocLinks.add(cDocLink); } //Insert insertDocLinks; List saveResultsLinks = Database.insert(insertDocLinks, false); <<<<<<< HEAD insertLog(saveResultsLinks, attachmentIds); } global void insertLog(List saveResults, Set attachmentIds) { Transaction_Log__c traLog = new Transaction_Log__c(); List> logMapList = new List>(); ======= insertLog(saveResultsLinks,attachmentIds); } global void insertLog(List saveResults,Set attachmentIds) { Transaction_Log__c traLog = new Transaction_Log__c(); List> logMapList = new List>(); >>>>>>> LEXCommunityLiJun for (Database.SaveResult result : saveResults) { String recordId = result.getId(); if (!result.isSuccess()) { for (Database.Error error : result.getErrors()) { <<<<<<< HEAD Map logMap = new Map(); String errorMsg = error.getMessage(); logMap.put('recordId', recordId); logMap.put('errorMsg', errorMsg); ======= Map logMap = new Map(); String errorMsg = error.getMessage(); logMap.put('recordId',recordId); logMap.put('errorMsg',errorMsg); >>>>>>> LEXCommunityLiJun logMapList.add(logMap); } } } <<<<<<< HEAD if (logMapList.size() > 0) { Map attachmentIdMap = new Map(); attachmentIdMap.put('attachmentIds', JSON.serialize(attachmentIds)); logMapList.add(attachmentIdMap); traLog.Response__c = JSON.serialize(logMapList); traLog.Module__c = 'Attachment COnvert Transaction '; insert traLog; } } global void finish(Database.BatchableContext BC) { } } ======= if(logMapList.size() > 0){ Map attachmentIdMap = new Map(); attachmentIdMap.put('attachmentIds',JSON.serialize(attachmentIds)); logMapList.add(attachmentIdMap); traLog.Response__c = JSON.serialize(logMapList); traLog.Module__c = 'Attachment COnvert Transaction '; Insert traLog; } } global void finish(Database.BatchableContext BC) { } } >>>>>>> LEXCommunityLiJun