Untitled
unknown
apex
2 years ago
5.8 kB
8
Indexable
global class Posigen_ProjectAttachmentSyncToJobBatch implements Database.Batchable<sObject>, Database.Stateful, Schedulable{ global String vSOQL; private List<sitetracker__Error_Report__c> listErrorReportIns = new List<sitetracker__Error_Report__c>(); private static Set<String> setJobStatusToAvoid = new Set<String>{'Completed', 'Cancelled'}; global Posigen_ProjectAttachmentSyncToJobBatch(String pSOQL){ if(pSOQL != null){ this.vSOQL = pSOQL; }else{ this.vSOQL = 'SELECT Id, (SELECT Id FROM sitetracker__Jobs__r WHERE sitetracker__Job_Status__c NOT IN: setJobStatusToAvoid),'+ ' (SELECT Id, sitetracker__ContentDocumentRecord__c FROM sitetracker__Attachments1__r WHERE sitetracker__ContentDocumentRecord__c != null)'+ ' FROM sitetracker__Project__c'+ ' WHERE Id IN (SELECT sitetracker__Project__c'+ ' FROM sitetracker__Job__c'+ ' WHERE sitetracker__Job_Status__c NOT IN: setJobStatusToAvoid)'+ ' AND Id IN (SELECT sitetracker__Proj__c'+ ' FROM sitetracker__Attachment__c'+ ' WHERE sitetracker__Proj__c != null)'; } } /** * @description Initial SOQL * @param Database.BatchableContext pBatchContext */ global Database.QueryLocator start(Database.BatchableContext pBatchContext){ return Database.getQueryLocator(this.vSOQL); } /** * @description Executes the necessary actions. * @param Database.BatchableContext pBatchContext * @param List<SObject> pScope */ global void execute(Database.BatchableContext pBatchContext, List<SObject> pScope){ if(pScope != null && !pScope.isEmpty()){ Set<Id> setContDocId = new Set<Id>(); Set<Id> setJobId = new Set<Id>(); for(sitetracker__Project__c forProj: (List<sitetracker__Project__c>) pScope){ // Filter the ContentDocumentIds if(!forProj.sitetracker__Attachments1__r.isEmpty()){ for(sitetracker__Attachment__c forAttach: forProj.sitetracker__Attachments1__r){ setContDocId.add(forAttach.sitetracker__ContentDocumentRecord__c); } } // Filter the Job Ids if(!forProj.sitetracker__Jobs__r.isEmpty()){ for(sitetracker__Job__c forJob: forProj.sitetracker__Jobs__r){ setJobId.add(forJob.Id); } } } if(!setContDocId.isEmpty() && !setJobId.isEmpty()){ // Query the Jobs to get the related ContentDocumentLink List<sitetracker__Job__c> listJobs = [ SELECT Id, (SELECT ContentDocumentId FROM ContentDocumentLinks) FROM sitetracker__Job__c WHERE Id IN: setJobId ]; for(sitetracker__Job__c forJob: listJobs){ // Remove the ContentDocumentId from the set where the file is already shared if(!forJob.ContentDocumentLinks.isEmpty()){ for(ContentDocumentLink forCDL: forJob.ContentDocumentLinks){ if(setContDocId.contains(forCDL.ContentDocumentId)){ setContDocId.remove(forCDL.ContentDocumentId); } } } } // If is not empty, meaning that there are still files to link if(!setContDocId.isEmpty()){ List<ContentDocumentLink> listCDLIns = new List<ContentDocumentLink>(); // Create the link with the Job for(sitetracker__Job__c forJob: listJobs){ for(Id forContDocId: setContDocId){ ContentDocumentLink link = new ContentDocumentLink( LinkedEntityId = forJob.Id, ContentDocumentId = forContDocId, ShareType = 'I' ); listCDLIns.add(link); } } if(!listCDLIns.isEmpty()){ List<Object> listCDLResult = Database.insert(listCDLIns); // Insert Error Reports sitetracker__Error_Report__c errorReport = Posigen_Utils.getErrorReportFromResult( listCDLResult, Posigen_ProjectAttachmentSyncToJobBatch.class.getName(), UserInfo.getUserId(), 'Failed to link the File from Project to Job: ' ); if (errorReport != null) { this.listErrorReportIns.add(errorReport); } } } } } } /** * @description Executes the necessary actions after all actions (final action). * @param Database.BatchableContext pBatchContext */ global void finish(Database.BatchableContext pBatchContext){ if(!this.listErrorReportIns.isEmpty()){ insert this.listErrorReportIns; } } /** * @description Default execute method. * @param SchedulableContext pContext */ global void execute(SchedulableContext pContext){ Database.executeBatch(new Posigen_ProjectAttachmentSyncToJobBatch(null), 10); } }
Editor is loading...