global without sharing class Batch_MappingQRCodeToAsset implements Database.Batchable { private String objectApiName = null; private String whereStr = null; public Batch_MappingQRCodeToAsset(){} public Batch_MappingQRCodeToAsset(String objectApiName, String whereStr) { this.objectApiName = objectApiName; this.whereStr = whereStr; } global Database.QueryLocator start(Database.BatchableContext BC) { String query = ''; switch on this.objectApiName { when null, '' { System.debug('No availabe object api name in start phase>>>>>>'); } when 'Asset' { query ='select id,QRId__c FROM Asset '; } when 'Rental_Apply__c' { query ='select id,QRId__c FROM Rental_Apply__c '; } when 'Consum_Apply__c' { query ='select id,QRId__c FROM Consum_Apply__c '; } when 'TransferApply__c' { query ='select id,QRId__c FROM TransferApply__c '; } when 'ReceivingNote__c' { query ='select id,QRId__c FROM ReceivingNote__c '; } when else { System.debug('open soon>>>>>>>>>'); } } if(String.isNotBlank(this.whereStr)) { query += this.whereStr; } System.debug('query>>>>>'+query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { if(scope.size() <= 0) { return; } switch on this.objectApiName { when 'Asset' { processAsset(scope); } when 'Rental_Apply__c' { processRentalApply(scope); } when 'Consum_Apply__c' { processConsumApply(scope); } when 'TransferApply__c' { processTransferApply(scope); } when 'ReceivingNote__c' { processReceivingNote(scope); } when else { System.debug('No availabe object api name in execution phase>>>>>>'); } } } global void processReceivingNote(list scope) { Set receivingNoteIdSet = new Set(); List updateReceivingNoteList = new List(); Set receivingNoteVersionIdSet = new Set(); for(Sobject targetObj : scope) { ReceivingNote__c receivingNoteObj = (ReceivingNote__c)targetObj; receivingNoteIdSet.add(receivingNoteObj.Id); } String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :receivingNoteIdSet and LinkedEntity.type = \'ReceivingNote__c\' and ContentDocument.Title like \'QRCode-%\''; List contentDocumentLinkList = Database.query(queryContentDocumentList); for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) { if (!receivingNoteVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) { ReceivingNote__c tempReceivingNote = new ReceivingNote__c(); tempReceivingNote.Id = contentDocumentLink.LinkedEntityId; tempReceivingNote.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId; updateReceivingNoteList.add(tempReceivingNote); receivingNoteVersionIdSet.add(contentDocumentLink.LinkedEntityId); } } if(updateReceivingNoteList.size() > 0) { List saveResultsContents = Database.update(updateReceivingNoteList, false); insertLog(saveResultsContents,receivingNoteIdSet); } } global void processTransferApply(list scope) { Set transferApplyIdSet = new Set(); List updateTransferApplyList = new List(); Set transferApplyVersionIdSet = new Set(); for(Sobject targetObj : scope) { TransferApply__c transferApplyObj = (TransferApply__c)targetObj; transferApplyIdSet.add(transferApplyObj.Id); } String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :transferApplyIdSet and LinkedEntity.type = \'TransferApply__c\' and ContentDocument.Title like \'QRCode-%\''; List contentDocumentLinkList = Database.query(queryContentDocumentList); for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) { if (!transferApplyVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) { TransferApply__c tempTransferApply = new TransferApply__c(); tempTransferApply.Id = contentDocumentLink.LinkedEntityId; tempTransferApply.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId; updateTransferApplyList.add(tempTransferApply); transferApplyVersionIdSet.add(contentDocumentLink.LinkedEntityId); } } if(updateTransferApplyList.size() > 0) { List saveResultsContents = Database.update(updateTransferApplyList, false); insertLog(saveResultsContents,transferApplyIdSet); } } global void processConsumApply(list scope) { Set consumApplyIdSet = new Set(); List updateConsumApplyList = new List(); Set consumApplyVersionIdSet = new Set(); for(Sobject targetObj : scope) { Consum_Apply__c consumApplyIdObj = (Consum_Apply__c)targetObj; consumApplyIdSet.add(consumApplyIdObj.Id); } String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :consumApplyIdSet and LinkedEntity.type = \'Consum_Apply__c\' and ContentDocument.Title like \'QRCode-%\''; List contentDocumentLinkList = Database.query(queryContentDocumentList); for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) { if (!consumApplyVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) { Consum_Apply__c tempConsumApply = new Consum_Apply__c(); tempConsumApply.Id = contentDocumentLink.LinkedEntityId; tempConsumApply.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId; updateConsumApplyList.add(tempConsumApply); consumApplyVersionIdSet.add(contentDocumentLink.LinkedEntityId); } } if(updateConsumApplyList.size() > 0) { List saveResultsContents = Database.update(updateConsumApplyList, false); insertLog(saveResultsContents,consumApplyIdSet); } } global void processRentalApply(list scope) { Set rentalApplyIdSet = new Set(); List updateRentalApplyList = new List(); Set rentalApplyVersionIdSet = new Set(); for(Sobject targetObj : scope) { Rental_Apply__c rentalApplyObj = (Rental_Apply__c)targetObj; rentalApplyIdSet.add(rentalApplyObj.Id); } String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :rentalApplyIdSet and LinkedEntity.type = \'Rental_Apply__c\' and ContentDocument.Title like \'QRCode-%\''; List contentDocumentLinkList = Database.query(queryContentDocumentList); for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) { if (!rentalApplyVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) { Rental_Apply__c tempRentalApply = new Rental_Apply__c(); tempRentalApply.Id = contentDocumentLink.LinkedEntityId; tempRentalApply.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId; updateRentalApplyList.add(tempRentalApply); rentalApplyVersionIdSet.add(contentDocumentLink.LinkedEntityId); } } if(updateRentalApplyList.size() > 0) { List saveResultsContents = Database.update(updateRentalApplyList, false); insertLog(saveResultsContents,rentalApplyIdSet); } } global void processAsset(list scope) { Set assetIdSet = new Set(); List updateAssetSet = new List(); Set assetWithVersionIdSet = new Set(); for(Sobject targetObj : scope) { Asset assetObj = (Asset)targetObj; assetIdSet.add(assetObj.Id); } String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :assetIdSet and LinkedEntity.type = \'Asset\' and ContentDocument.Title = \'QRCode\''; List contentDocumentLinkList = Database.query(queryContentDocumentList); for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) { if (!assetWithVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) { Asset tempAsset = new Asset(); tempAsset.Id = contentDocumentLink.LinkedEntityId; tempAsset.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId; updateAssetSet.add(tempAsset); assetWithVersionIdSet.add(contentDocumentLink.LinkedEntityId); } } if(updateAssetSet.size() > 0) { List saveResultsContents = Database.update(updateAssetSet, false); insertLog(saveResultsContents,assetIdSet); } } global void insertLog(List saveResults,Set mainIds) { 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 mainIdMap = new Map(); mainIdMap.put(this.objectApiName + 'Ids',JSON.serialize(mainIds)); logMapList.add(mainIdMap); traLog.Response__c = JSON.serialize(logMapList); traLog.Module__c = 'QR mapping Convert Transaction'; Insert traLog; } } global void finish(Database.BatchableContext BC) { } }