global without sharing class Batch_Ali_FixAttachmentToFilesBP implements Database.Batchable<sObject>{
|
// Id batchJobId = Database.executeBatch(new Batch_FixAttachmentToFilesBP('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 String whereS = null; // 20231110 ljh add
|
private Set<String> parentIds = new Set<String>();//Add by Li Jun 20230703
|
public Batch_Ali_FixAttachmentToFilesBP(String objectType, Datetime startTime, Datetime endTime) {
|
creStartDate = startTime;
|
creEndDate = endTime;
|
objectApiName = objectType;
|
}
|
// 20231110 ljh add start
|
public Batch_Ali_FixAttachmentToFilesBP(String objectType,Datetime startTime, Datetime endTime,String myWhere) {
|
objectApiName = objectType;
|
creStartDate = startTime;
|
creEndDate = endTime;
|
whereS = myWhere;
|
}
|
// 20231110 ljh add end
|
//Add by Li Jun 20230703 Start
|
public Batch_Ali_FixAttachmentToFilesBP(Set<String> parentIds) {
|
this.parentIds = parentIds;
|
}
|
//Add by Li Jun 20230703 End
|
global Database.QueryLocator start(Database.BatchableContext BC) {
|
String query = '';
|
switch on objectApiName {
|
when null, '' {
|
query = '' ;
|
System.debug('No availabe object api name in start phase>>>>>>');
|
}
|
when 'Asset' {
|
query ='select id,Name,QrId__c FROM ';
|
}
|
when else {
|
query ='select id,Name,QrId__c,BRId__c FROM ';
|
}
|
}
|
query += objectApiName;
|
// query +=' WHERE CreatedDate >=:creStartDate AND CreatedDate <:creEndDate ';
|
query +=' WHERE Id =\'02iC8000000DAfRIAW\'';
|
if(String.isNotBlank(whereS)){
|
query += whereS;
|
}
|
query += ' Order by CreatedDate desc';
|
System.debug('query>>>>>'+query);
|
return Database.getQueryLocator(query);
|
}
|
|
global void execute(Database.BatchableContext BC, list<Sobject> scope) {
|
if(scope.size() <= 0) {
|
return;
|
}
|
switch on objectApiName {
|
when 'Asset' {
|
processAsset(scope);
|
}
|
when else {
|
System.debug('No availabe object api name in execution phase>>>>>>');
|
}
|
}
|
|
}
|
|
global void processAsset(list<Sobject> scope) {
|
Set<String> SetAssetQRId = new Set<String>();
|
List<ContentVersion> updateContents = new List<ContentVersion>();
|
for(Sobject targetObj : scope) {
|
Asset assetObj = (Asset)targetObj;
|
SetAssetQRId.add(assetObj.QrId__c);
|
ContentVersion cVersion = new ContentVersion();
|
cVersion.Id = assetObj.QrId__c;
|
cVersion.Title = 'QRCode-' + assetObj.Name;
|
cVersion.PathOnClient = 'QRCode-' + assetObj.Name + '.jpg';
|
System.debug('Asset new Title>>>>' + cVersion.Title);
|
updateContents.add(cVersion);
|
}
|
if(updateContents.isEmpty()){
|
return;
|
}
|
List<Database.SaveResult> saveResultsContents = Database.update(updateContents, false);
|
insertLog(saveResultsContents,SetAssetQRId);
|
|
//process ContentDocumentLink
|
// set<Id> contentIds = new set<Id>();
|
// for(ContentVersion cv : updateContents){
|
// contentIds.add(cv.id);
|
// }
|
// List<ContentVersion> conDocuments = [SELECT ContentDocumentId, Title,PathOnClient FROM ContentVersion WHERE Id in: contentIds];
|
// List<ContentDocumentLink> updateDocLinks = new List<ContentDocumentLink>();
|
// 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';
|
// updateDocLinks.add(cDocLink);
|
// }
|
// List<Database.SaveResult> saveResultsLinks = Database.update(updateDocLinks, false);
|
// insertLog(saveResultsLinks,SetAssetQRId);
|
}
|
|
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>>();
|
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>();
|
String errorMsg = error.getMessage();
|
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));
|
logMapList.add(attachmentIdMap);
|
traLog.Response__c = JSON.serialize(logMapList);
|
traLog.Module__c = 'Attachment COnvert Transaction ';
|
Insert traLog;
|
}
|
}
|
global void finish(Database.BatchableContext BC) {
|
|
}
|
}
|