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
DaveGCDaveGC 

Need to delete all members of a campaign given a campaign ID

Hi All, 

I need to remove all campaign members from within an apex class (unmanaged package), I can get get campaign ID, but I can't find what is the method to remove all members, can someone point me in the right direction?

Thanks!

-Dave
Best Answer chosen by DaveGC
Prateek Singh SengarPrateek Singh Sengar
Hi Dave,
Did you tried something like
 
List<CampaignMember> campMemToDel =  SELECT Id FROM CampaignMember WHERE CampaignId in : YOUR_CAMPAIGN_ID_SET];

if(campMemToDel != nulll && campMemToDel.size()>0)
{
 delete campMemToDel;
}
YOUR_CAMPAIGN_ID_SET is your collection that contain the campaign id.
 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Dave,
Did you tried something like
 
List<CampaignMember> campMemToDel =  SELECT Id FROM CampaignMember WHERE CampaignId in : YOUR_CAMPAIGN_ID_SET];

if(campMemToDel != nulll && campMemToDel.size()>0)
{
 delete campMemToDel;
}
YOUR_CAMPAIGN_ID_SET is your collection that contain the campaign id.
 
This was selected as the best answer
DaveGCDaveGC
Hi, the app developer just replied (free app) and the code is basically the same as yours: Yours would work as well, thanks!
delete [ SELECT id FROM CampaignMember WHERE campaignId = :campaignId ];
Rishabh Patel 1Rishabh Patel 1

@Prateek! Your code looks perfect. I am pretty new on salesforce development  . Can you tell me how to use this code. I mean add in the apex class or trigger ?

That would be a great help