I am running the following batches and for some reason, the system keeps erroring out. I think that I might be running too many batches - or I just do not realize something basic.
global class SDR_Calculator implements Schedulable
{
public Integer batchSize = 100;
global void execute(SchedulableContext sc)
{
List<Id> SDR_IDs = new List<Id>(new Map<Id, User>([SELECT Id FROM user WHERE UserRoleId = '00E6A000000UW7gUAG' and IsActive = true]).keySet());
// TR 90
String Contacts_Created_TR_120_Query = 'SELECT CreatedByID SDR_ID, COUNT( id ) result FROM Contact Where CreatedDate = LAST_N_DAYS:120 AND CreatedById in :ListOfIDs GROUP BY CreatedById';
DynamicBatchApexAR batch1_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Contacts_Created_TR_120__c', Contacts_Created_TR_120_Query, SDR_IDs);
String Accounts_Created_TR_120_Query = 'SELECT CreatedByID SDR_ID, COUNT( id ) result FROM Account Where CreatedDate = LAST_N_DAYS:120 AND CreatedById in :ListOfIDs GROUP BY CreatedById';
DynamicBatchApexAR batch2_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Accounts_Created_TR_120__c', Accounts_Created_TR_120_Query, SDR_IDs);
String Contacts_Reached_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT_DISTINCT( whoid ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch3_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Contacts_Reached_TR_120__c', Contacts_Reached_TR_120_Query, SDR_IDs);
String Accounts_Reached_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT_DISTINCT( accountid ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch4_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Accounts_Reached_TR_120__c', Accounts_Reached_TR_120_Query, SDR_IDs);
String Manual_Emails_Tr_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Subject like \'%[Out]%\' AND ( Subject like \'%[Manual]%\' OR Subject like \'%[Gmail]%\' ) AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch5_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Manual_Emails_TR_120__c', Manual_Emails_Tr_120_Query, SDR_IDs);
String Emails_Tr_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Subject like \'%[Out]%\' AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch6_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Emails_TR_120__c', Emails_Tr_120_Query, SDR_IDs);
String Calls_Tr_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Subject like \'%[Outbound]%\' AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch7_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Calls_TR_120__c', Calls_Tr_120_Query, SDR_IDs);
String Other_Tasks_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Outreach_Task_Type__c != NULL AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch8_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Other_Tasks_TR_120__c', Other_Tasks_TR_120_Query, SDR_IDs);
String Email_Opens_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Opened_At__c != NULL AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch9_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Email_Opens_TR_120__c', Email_Opens_TR_120_Query, SDR_IDs);
String Email_Clicks_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Number_Of_Clicks__c > 0 AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch10_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Email_Clicks_TR_120__c', Email_Clicks_TR_120_Query, SDR_IDs);
String Email_Replies_TR_120_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_N_DAYS:120 AND Subject like \'%[In]%\' AND ( NOT Subject like \'%Automatic%\' ) AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch11_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Email_Replies_TR_120__c', Email_Replies_TR_120_Query, SDR_IDs);
String Demo_Sets_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND ( RecordType.Name = \'Pre Opportunity\' OR RecordType.Name = \'New Business\' ) AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch12_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Demo_Sets_TR_120__c', Demo_Sets_TR_120_Query, SDR_IDs);
String Demo_No_Shows_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND RecordType.Name = \'Pre Opportunity\' AND Rejection_Reason__c = \'No Show\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch13_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Demo_No_Shows_TR_120__c', Demo_No_Shows_TR_120_Query, SDR_IDs);
String Demo_Completes_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND Demo_Completed__c = true AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch14_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Demo_Completes_TR_120__c', Demo_Completes_TR_120_Query, SDR_IDs);
String Requalifying_Demos_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND Recordtype.Name = \'Pre Opportunity\' AND reengagement__c = \'Yes\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch15_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Requalifying_Demos_TR_120__c', Requalifying_Demos_TR_120_Query, SDR_IDs);
String Unqualified_Demos_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND Recordtype.Name = \'Pre Opportunity\' AND rejection_reasion__c = \'Unqualified Account\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch16_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Unqualified_Demos_TR_120__c', Unqualified_Demos_TR_120_Query, SDR_IDs);
String Qualified_Demos_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND Recordtype.Name = \'New Business\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch17_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Qualified_Demos_TR_120__c', Qualified_Demos_TR_120_Query, SDR_IDs);
String Closed_Won_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND Recordtype.Name = \'New Business\' AND IsWon = true AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch18_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Closed_Won_TR_120__c', Closed_Won_TR_120_Query, SDR_IDs);
String Closed_Lost_TR_120_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_N_DAYS:120 AND Recordtype.Name = \'New Business\' AND IsClosed = true AND IsWon = false AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch19_TR120 = new DynamicBatchApexAR('SDR_ID', 'result', 'Closed_Lost_TR_120__c', Closed_Lost_TR_120_Query, SDR_IDs);
// TR 120 Batches
Database.executeBatch(batch1_TR120, batchSize);
Database.executeBatch(batch2_TR120, batchSize);
Database.executeBatch(batch3_TR120, batchSize);
Database.executeBatch(batch4_TR120, batchSize);
Database.executeBatch(batch5_TR120, batchSize);
Database.executeBatch(batch6_TR120, batchSize);
Database.executeBatch(batch7_TR120, batchSize);
Database.executeBatch(batch8_TR120, batchSize);
Database.executeBatch(batch9_TR120, batchSize);
Database.executeBatch(batch10_TR120, batchSize);
Database.executeBatch(batch11_TR120, batchSize);
Database.executeBatch(batch12_TR120, batchSize);
Database.executeBatch(batch13_TR120, batchSize);
Database.executeBatch(batch14_TR120, batchSize);
Database.executeBatch(batch15_TR120, batchSize);
Database.executeBatch(batch16_TR120, batchSize);
Database.executeBatch(batch17_TR120, batchSize);
Database.executeBatch(batch18_TR120, batchSize);
Database.executeBatch(batch19_TR120, batchSize);
// TR 90
String Contacts_Created_TR_90_Query = 'SELECT CreatedByID SDR_ID, COUNT( id ) result FROM Contact Where CreatedDate = LAST_90_DAYS AND CreatedById in :ListOfIDs GROUP BY CreatedById';
DynamicBatchApexAR batch1_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Contacts_Created_TR_90__c', Contacts_Created_TR_90_Query);
String Accounts_Created_TR_90_Query = 'SELECT CreatedByID SDR_ID, COUNT( id ) result FROM Account Where CreatedDate = LAST_90_DAYS AND CreatedById in :ListOfIDs GROUP BY CreatedById';
DynamicBatchApexAR batch2_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Accounts_Created_TR_90__c', Accounts_Created_TR_90_Query);
String Contacts_Reached_TR_90_Query = 'SELECT Ownerid SDR_ID, COUNT_DISTINCT( whoid ) result FROM Task Where ActivityDate = LAST_90_DAYS AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch3_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Contacts_Reached_TR_90__c', Contacts_Reached_TR_90_Query);
String Accounts_Reached_TR_90_Query = 'SELECT Ownerid SDR_ID, COUNT_DISTINCT( accountid ) result FROM Task Where ActivityDate = LAST_90_DAYS AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch4_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Accounts_Reached_TR_90__c', Accounts_Reached_TR_90_Query);
String Manual_Emails_Tr_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Subject like \'%[Out]%\' AND ( Subject like \'%[Manual]%\' OR Subject like \'%[Gmail]%\' ) AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch5_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Manual_Emails_TR_90__c', Manual_Emails_Tr_90_Query);
String Emails_Tr_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Subject like \'%[Out]%\' AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch6_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Emails_TR_90__c', Emails_Tr_90_Query);
String Calls_Tr_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Subject like \'%[Outbound]%\' AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch7_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Calls_TR_90__c', Calls_Tr_90_Query);
String Other_Tasks_TR_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Outreach_Task_Type__c != NULL AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch8_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Other_Tasks_TR_90__c', Other_Tasks_TR_90_Query);
String Email_Opens_TR_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Opened_At__c != NULL AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch9_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Email_Opens_TR_90__c', Email_Opens_TR_90_Query);
String Email_Clicks_TR_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Number_Of_Clicks__c > 0 AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch10_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Email_Clicks_TR_90__c', Email_Clicks_TR_90_Query);
String Email_Replies_TR_90_Query = 'SELECT Ownerid SDR_ID, COUNT( id ) result FROM Task Where ActivityDate = LAST_90_DAYS AND Subject like \'%[In]%\' AND ( NOT Subject like \'%Automatic%\' ) AND OwnerId in :ListOfIDs GROUP BY Ownerid';
DynamicBatchApexAR batch11_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Email_Replies_TR_90__c', Email_Replies_TR_90_Query);
String Demo_Sets_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND ( RecordType.Name = \'Pre Opportunity\' OR RecordType.Name = \'New Business\' ) AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch12_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Demo_Sets_TR_90__c', Demo_Sets_TR_90_Query);
String Demo_No_Shows_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND RecordType.Name = \'Pre Opportunity\' AND Rejection_Reason__c = \'No Show\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch13_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Demo_No_Shows_TR_90__c', Demo_No_Shows_TR_90_Query);
String Demo_Completes_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND Demo_Completed__c = true AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch14_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Demo_Completes_TR_90__c', Demo_Completes_TR_90_Query);
String Requalifying_Demos_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND Recordtype.Name = \'Pre Opportunity\' AND reengagement__c = \'Yes\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch15_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Requalifying_Demos_TR_90__c', Requalifying_Demos_TR_90_Query);
String Unqualified_Demos_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND Recordtype.Name = \'Pre Opportunity\' AND rejection_reasion__c = \'Unqualified Account\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch16_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Unqualified_Demos_TR_90__c', Unqualified_Demos_TR_90_Query);
String Qualified_Demos_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND Recordtype.Name = \'New Business\' AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch17_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Qualified_Demos_TR_90__c', Qualified_Demos_TR_90_Query);
String Closed_Won_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND Recordtype.Name = \'New Business\' AND IsWon = true AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch18_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Closed_Won_TR_90__c', Closed_Won_TR_90_Query);
String Closed_Lost_TR_90_Query = 'SELECT SDR_Owner__c SDR_ID, COUNT(id) result FROM Opportunity where CreatedDate = LAST_90_DAYS AND Recordtype.Name = \'New Business\' AND IsClosed = true AND IsWon = false AND SDR_Owner__c in :ListOfIDs GROUP BY SDR_Owner__c';
DynamicBatchApexAR batch19_TR90 = new DynamicBatchApexAR('SDR_ID', 'result', 'Closed_Lost_TR_90__c', Closed_Lost_TR_90_Query);
// TR 90 Batches
Database.executeBatch(batch1_TR90, batchSize);
Database.executeBatch(batch2_TR90, batchSize);
Database.executeBatch(batch3_TR90, batchSize);
Database.executeBatch(batch4_TR90, batchSize);
Database.executeBatch(batch5_TR90, batchSize);
Database.executeBatch(batch6_TR90, batchSize);
Database.executeBatch(batch7_TR90, batchSize);
Database.executeBatch(batch8_TR90, batchSize);
Database.executeBatch(batch9_TR90, batchSize);
Database.executeBatch(batch10_TR90, batchSize);
Database.executeBatch(batch11_TR90, batchSize);
Database.executeBatch(batch12_TR90, batchSize);
Database.executeBatch(batch13_TR90, batchSize);
Database.executeBatch(batch14_TR90, batchSize);
Database.executeBatch(batch15_TR90, batchSize);
Database.executeBatch(batch16_TR90, batchSize);
Database.executeBatch(batch17_TR90, batchSize);
Database.executeBatch(batch18_TR90, batchSize);
Database.executeBatch(batch19_TR90, batchSize);
}
Then here is the batch class
global class DynamicBatchApexAR implements Database.Batchable {
public String sObjectIdKey;
public String sObjectResultKey;
public String sObjectFieldToUpdate;
public String query;
public List<Id> ListOfIDs;
global DynamicBatchApexAR(String arIdKey, String arResultKey, String fieldToUpdate, String soqlQuery, List<Id> whereInClause )
{
sObjectIdKey = arIdKey;
sObjectResultKey = arResultKey;
sObjectFieldToUpdate = fieldToUpdate;
query = soqlQuery;
ListOfIDs = whereInClause;
}
global DynamicBatchApexAR(String arIdKey, String arResultKey, String fieldToUpdate, String soqlQuery )
{
sObjectIdKey = arIdKey;
sObjectResultKey = arResultKey;
sObjectFieldToUpdate = fieldToUpdate;
query = soqlQuery;
}
global Iterable<AggregateResult> start(Database.BatchableContext BC)
{
return ListOfIDs == null ? new BulkIterable(query) : new BulkIterable(query, ListOfIDs);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();
for(sObject sObj: scope)
{
AggregateResult ar = (AggregateResult)sObj;
ID sObjectId = (ID)ar.get(sObjectIdKey);
Decimal arResult = (Decimal)ar.get(sObjectResultKey);
Map<String, Object> sObjectFieldsToUpdate = new Map<String, Object>{
sObjectFieldToUpdate => arResult
};
sObjectUpdater.getUpdateSObject(sObjectId, sObjectFieldsToUpdate);
}
sObjectUpdater.updateSObjects();
}
global void finish(Database.BatchableContext BC)
{
}
}
Here is the iterable
global with sharing class BulkIterable implements Iterable { String query; List ListOfIDs;
global BulkIterable(String soql, List<Id> WhereInClause)
{
query = soql;
ListOfIDs = WhereInClause;
}
global BulkIterable(String soql)
{
query = soql;
}
global Iterator<AggregateResult> Iterator(){
return ListOfIDs == null ? new BulkIterator(query) : new BulkIterator(query, ListOfIDs);
}
}
Here is the iterator
global class BulkIterator implements Iterator<AggregateResult> {
AggregateResult[] results { get;set; }
Integer index { get;set; }
List<Id> ListOfIDs;
global BulkIterator(String query, List<Id> WhereInClause)
{
index = 0;
ListOfIDs = WhereInClause;
results = Database.query(query);
}
global BulkIterator(String query) {
index = 0;
results = Database.query(query);
}
global Boolean hasNext(){
return results != null && !results.isEmpty() && index < results.size();
}
global AggregateResult next() {
return results[index++];
}
}
Is the error because I am running too many batches?