You need to sign in to do that
Don't have an account?
Lorenzo
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
Can i make it with a single query? How can i manage the complex query with Batch class? Thank you for your support
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
Batch
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