I've run into a pretty maddening issue. The simple batch class below looks up certain campaign members and deletes them. It works fine, but when I comment out the debug statement on line 29, the delete operation does not occur.
I've tested it dozens of times, did my very best to ensure that all other variables are kept constant, and I'm still running into the same issue. At this point I'm preparing to deploy with the debug call still there, but if anyone has any ideas, I'd be very curious what's going on.
global class HustleCampaignDeleteBatchable implements Database.Batchable<SObject>, Schedulable {
//Create list to store campaign ids
global List<String> hustleCampaigns = new List<String>();
global List<CampaignMember> campaignMembersToDelete = new List<CampaignMember>();
global void execute(SchedulableContext sc) {
Database.executeBatch(this);
}
global HustleCampaignDeleteBatchable() {
//Query for HustleGroupInterest Custom Metadata Type
Hustle_Group_Update__mdt[] hustleGroupUpdate = [SELECT Campaign_Id__c FROM Hustle_Group_Update__mdt];
//Populate Hustle Campaign List
for(Hustle_Group_Update__mdt hgu : hustleGroupUpdate) {
hustleCampaigns.add(hgu.Campaign_Id__c);
}
//System.debug('hustleCampaigns: ' + hustleCampaigns);
}
global Database.QueryLocator start(Database.BatchableContext context) {
//Select all Campaign Members in Hustle campaigns
return Database.getQueryLocator([SELECT Id, CampaignId FROM CampaignMember WHERE CampaignId IN :hustleCampaigns]);
}
global void execute(Database.BatchableContext context, List<CampaignMember> scope) {
//Loop through all campaign members and add those in a Hustle campaign to a delete list
for(CampaignMember cm : scope) {
if (hustleCampaigns.contains(cm.CampaignId)){
campaignMembersToDelete.add(cm);
}
}
delete campaignMembersToDelete;
}
global void finish(Database.BatchableContext context) {
//Call the batch process to repopulate Hustle Campaigns
//Database.executeBatch(new HustleCampaignUpdateBatchable());
}
}