function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LorenzoLorenzo 

Write a Batch class to delete Campaign Member

I need to write a Batch class for Campaign members delete. I need to find the campaign member with a relationship with contact/lead associated with Campaign with Completed/Aborted status.
Can i make it with a single query? How can i manage the complex query with Batch class? Thank you for your support
Best Answer chosen by Lorenzo
Maharajan CMaharajan C
Hi Lorenzo,

Please find the updated query:

SELECT Id,LeadId,ContactId FROM CampaignMember where (Campaign.Status='Completed' OR Campaign.Status='Aborted') AND (LeadId != Null OR ContactId != Null)


Batch Class:

global class deleteCampaignMembers implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator('SELECT Id,LeadId,ContactId FROM CampaignMember where (Campaign.Status='Completed' OR Campaign.Status='Aborted') AND (LeadId != Null OR ContactId != Null)');
    }

    global void execute(Database.BatchableContext BC,List<CampaignMember> scope)
    {
        Delete scope;
    }

    global void finish(Database.BatchableContext BC)
    {
        
    }
}

Schedule Class:

global class deleteCampaignMembersScheduable implements Schedulable
{
    global void execute(SchedulableContext ctx) 
    {                
        id batchinstanceid = database.executeBatch(new deleteCampaignMembers());  
    }   
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj

All Answers

Raj VakatiRaj Vakati
SOQL 
Select Id ,ContactId , LeadId from CampaignMember where Campaign.Status='Completed' OR  Campaign.Status='Aborted' OR ContactId !=NULL OR LEADID!=NULL

Batch
 
global class BatchAddToCampaign implements Database.Batchable<sObject>,  Database.Stateful
{ 

global Database.QueryLocator start(Database.BatchableContext BC) {
	return Database.getQueryLocator([Select Id ,ContactId , LeadId from CampaignMember where Campaign.Status='Completed' OR  Campaign.Status='Aborted' OR ContactId !=NULL OR LEADID!=NULL]);
}

global void execute(Database.BatchableContext BC, List<Sobject> scope) {
  delete scope ;    
}

global void finish(Database.BatchableContext BC) {
   
}
}

 
Maharajan CMaharajan C
Hi Lorenzo,

Please find the updated query:

SELECT Id,LeadId,ContactId FROM CampaignMember where (Campaign.Status='Completed' OR Campaign.Status='Aborted') AND (LeadId != Null OR ContactId != Null)


Batch Class:

global class deleteCampaignMembers implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator('SELECT Id,LeadId,ContactId FROM CampaignMember where (Campaign.Status='Completed' OR Campaign.Status='Aborted') AND (LeadId != Null OR ContactId != Null)');
    }

    global void execute(Database.BatchableContext BC,List<CampaignMember> scope)
    {
        Delete scope;
    }

    global void finish(Database.BatchableContext BC)
    {
        
    }
}

Schedule Class:

global class deleteCampaignMembersScheduable implements Schedulable
{
    global void execute(SchedulableContext ctx) 
    {                
        id batchinstanceid = database.executeBatch(new deleteCampaignMembers());  
    }   
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj
This was selected as the best answer