From 5b5c1e16deaa3a9d6d0ed1ffca390655ed103df7 Mon Sep 17 00:00:00 2001 From: buli <137736985@qq.com> Date: 星期五, 14 七月 2023 14:56:14 +0800 Subject: [PATCH] lex community --- force-app/main/default/classes/Batch_FixAttachmentToFiles.cls | 79 +++++++++++++++++++-------------------- 1 files changed, 39 insertions(+), 40 deletions(-) diff --git a/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls b/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls index 1d917ed..ccb9821 100644 --- a/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls +++ b/force-app/main/default/classes/Batch_FixAttachmentToFiles.cls @@ -1,9 +1,9 @@ -global without sharing class Batch_FixAttachmentToFiles implements Database.Batchable<sObject> { +global without sharing class Batch_FixAttachmentToFiles implements Database.Batchable<sObject>{ // 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<String> parentIds = new Set<String>(); //Add by Li Jun 20230703 + private String objectApiName = null; + private Set<String> parentIds = new Set<String>();//Add by Li Jun 20230703 public Batch_FixAttachmentToFiles(String objectType, Datetime startTime, Datetime endTime) { creStartDate = startTime; creEndDate = endTime; @@ -13,10 +13,10 @@ public Batch_FixAttachmentToFiles(Set<String> parentIds) { this.parentIds = parentIds; } - //Add by Li Jun 20230703 End + //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) { + if(parentIds.size() > 0){ queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE ParentId in:parentIds'; } return Database.getQueryLocator(queryObject); @@ -24,73 +24,72 @@ global void execute(Database.BatchableContext BC, List<Attachment> scope) { List<ContentVersion> insertContents = new List<ContentVersion>(); - Map<string, id> nameParentMaps = new Map<string, id>(); - Set<String> attachmentIds = new Set<String>(); + Map<string,id> nameParentMaps = new Map<string,id>(); + Set<String> attachmentIds = new Set<String>(); for (Attachment att : scope) { - nameParentMaps.put(att.Name, att.ParentId); - attachmentIds.add(att.Id); + 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.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); + 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()) { + if(insertContents.isEmpty()){ return; } //Insert insertContents; List<Database.SaveResult> saveResultsContents = Database.insert(insertContents, false); - insertLog(saveResultsContents, attachmentIds); - set<Id> contentIds = new Set<Id>(); - for (ContentVersion cv : insertContents) { + insertLog(saveResultsContents,attachmentIds); + set<Id> contentIds = new set<Id>(); + for(ContentVersion cv : insertContents){ contentIds.add(cv.id); } - List<ContentVersion> conDocuments = [ - SELECT ContentDocumentId, Title - FROM ContentVersion - WHERE Id IN :contentIds - ]; - List<ContentDocumentLink> insertDocLinks = new List<ContentDocumentLink>(); - for (ContentVersion cv : conDocuments) { + List<ContentVersion> conDocuments = [SELECT ContentDocumentId, Title,PathOnClient FROM ContentVersion WHERE Id in: contentIds]; + List<ContentDocumentLink> insertDocLinks = new List<ContentDocumentLink>(); + 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. + 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<Database.SaveResult> saveResultsLinks = Database.insert(insertDocLinks, false); - insertLog(saveResultsLinks, attachmentIds); + insertLog(saveResultsLinks,attachmentIds); } - global void insertLog(List<Database.SaveResult> saveResults, Set<String> attachmentIds) { + global void insertLog(List<Database.SaveResult> saveResults,Set<String> attachmentIds) { Transaction_Log__c traLog = new Transaction_Log__c(); - List<Map<String, String>> logMapList = new List<Map<String, String>>(); + List<Map<String,String>> logMapList = new List<Map<String,String>>(); for (Database.SaveResult result : saveResults) { String recordId = result.getId(); if (!result.isSuccess()) { for (Database.Error error : result.getErrors()) { - Map<String, String> logMap = new Map<String, String>(); + Map<String,String> logMap = new Map<String,String>(); String errorMsg = error.getMessage(); - logMap.put('recordId', recordId); - logMap.put('errorMsg', errorMsg); + logMap.put('recordId',recordId); + logMap.put('errorMsg',errorMsg); logMapList.add(logMap); } } } - if (logMapList.size() > 0) { - Map<String, String> attachmentIdMap = new Map<String, String>(); - attachmentIdMap.put('attachmentIds', JSON.serialize(attachmentIds)); + if(logMapList.size() > 0){ + Map<String,String> attachmentIdMap = new Map<String,String>(); + attachmentIdMap.put('attachmentIds',JSON.serialize(attachmentIds)); logMapList.add(attachmentIdMap); traLog.Response__c = JSON.serialize(logMapList); traLog.Module__c = 'Attachment COnvert Transaction '; - insert traLog; + Insert traLog; } } + + global void finish(Database.BatchableContext BC) { + } -} +} \ No newline at end of file -- Gitblit v1.9.1