global with sharing class AEExportHistoriesBatchProcess implements Database.Batchable, Database.Stateful, schedulable, Database.RaisesPlatformEvents { public Export__c exp; public Integer Attachmentcount; String exportid = ''; public Integer processedcount; global database.querylocator start(Database.BatchableContext BC) { return Database.getQueryLocator( [ SELECT ID, Exported_Record_Count__c, Export_HIstory_Processed__c, Export_Type__c, Exported_By__c, createddate FROM Export__c WHERE Export_HIstory_Processed__c = FALSE AND Export_Type__c = 'AE Export' ORDER BY createddate ASC LIMIT 1 ] ); } global void execute(Database.BatchableContext BC, List scope) { List exphis = new List(); List emailslist = new List(); List uniqueemailslist = new List(); Map fansids = new Map(); Map fansemails = new Map(); try { if (scope != null) { exp = scope.get(0); } List attachmentsToProcess = [ SELECT ID, Body, Name FROM Attachment WHERE parentId = :exp.id AND Description = 'FALSE' LIMIT 1 ]; if (attachmentsToProcess.size() < 1) { Attachmentcount = 0; } system.debug('Attachment Size:' + attachmentsToProcess.size()); if (attachmentsToProcess.size() > 0) { Attachment attachment1 = attachmentsToProcess.get(0); List> parsedCSV = new List>(); parsedCSV = FormatUtils.parseCSV(attachment1.Body.ToString(), true); system.debug('parsedCSV Size:' + parsedCSV); for (List line : parsedCSV) { if (line != null && line.size() > 0) { emailslist.add(line[0].trim()); } } system.debug('Emails:' + emailslist); for (String s : emailslist) { if (s != '' && s != null) { uniqueemailslist.add(s); } } Integer emailssize = uniqueemailslist.Size(); processedcount = (Integer) exp.Exported_Record_Count__c; processedcount = processedcount + emailssize; List fansfromEmails = [SELECT id, Email__c FROM Fan__c WHERE Email__c IN :uniqueemailslist]; system.debug('Emails size:' + fansfromEmails.size()); for (Fan__c fan : fansfromEmails) { fansids.put(fan.Email__c, fan.Id); fansemails.put(fan.Email__c, fan.Email__c); } if (uniqueemailslist.size() > 0) { for (String email : uniqueemailslist) { Export_History__c exhis = new Export_History__c(); exhis.Email__c = email; exhis.Fan_ID__c = fansids.get(email); exhis.Export_ID__c = exp.Id; exhis.Export_Type__c = exp.Export_Type__c; exhis.Exported_By__c = exp.Exported_By__c; exhis.Date_Exported__c = exp.createddate.date(); Id RecTypeId = Schema.SObjectType.Export_History__c.getRecordTypeInfosByName() .get('AEExport') .getRecordTypeId(); exhis.Recordtypeid = RecTypeId; exphis.add(exhis); } } exp.Exported_Record_Count__c = processedcount; update exp; attachment1.Description = 'TRUE'; update attachment1; } insert exphis; } catch (Exception e) { SMELogger.logError(e); } } global void finish(Database.BatchableContext BC) { try { if (exp != null) { if (Attachmentcount == 0) { for (CronTrigger ct : [ SELECT Id, CronJobDetail.Name FROM CronTrigger WHERE CronJobDetail.Name LIKE 'AEExportHistoriesBatchProcess%' ORDER BY Createddate DESC LIMIT 1 ]) { system.abortJob(ct.id); } exp.Export_HIstory_Processed__c = true; update exp; System.schedule( 'AEExportHistoriesBatchProcessJob', '0 15 * * * ?', new AEExportHistoriesBatchProcess() ); } else { System.Debug('Export Record Id Finish:' + exportid); AEExportHistoriesBatchProcess s = new AEExportHistoriesBatchProcess(); Database.executebatch(s); } } else { for (CronTrigger ct : [ SELECT Id, CronJobDetail.Name FROM CronTrigger WHERE CronJobDetail.Name LIKE 'AEExportHistoriesBatchProcess%' ORDER BY Createddate DESC LIMIT 1 ]) { system.abortJob(ct.id); } System.schedule( 'AEExportHistoriesBatchProcessJob', '0 15 * * * ?', new AEExportHistoriesBatchProcess() ); } } catch (Exception e) { } } global void execute(SchedulableContext sc) { AEExportHistoriesBatchProcess s = new AEExportHistoriesBatchProcess(); Database.executebatch(s); } }