I need to create a batch that automates the creation of Cases from a Campaign by taking only the members related to a campaign that are in 'To send' status. (this batch should run after pressing a button in a campaign object and process that particular campaign)

Each campaign can have related different members.

my draft code :

global class CreateRicontatto implements Database.Batchable<sObject> {
    global Database.QueryLocator start (Database.BatchableContext BC){
        String id = '7010J000001HCkjQAG';
        String query = 'SELECT Campaign.Id, Status, Campaign.Type__c, Campaign.Subject__c, Name, Campaign.Data_Chiamata__c, Campaign.Description FROM CampaignMember WHERE Campaign.Id :=id';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<CampaignMember> scope){
        List<Case> ricontatto = new List<Case>();
        For(CampaignMember c : scope){
            if(c.Status == 'To Send'){
                Case casi = new Case();
                casi.RecordTypeId = '0120Y000000FbCuQAK';
                casi.Campaign__c = c.Campaign.Id;
                casi.Type = c.Campaign.Type__c;
                casi.Description = c.Campaign.Description;
                casi.Subject = c.Campaign.Subject__c;
                casi.DataChiamata__c = c.Campaign.Data_Chiamata__c;
            insert ricontatto;
global void finish (Database.BatchableContext BC){

thank you for any help.

  • What's your question? You've provided some code, but I'm not sure where you're stuck?
    – sfdcfox
    Commented Jun 20, 2022 at 12:38
  • I would like to know if it would be possible to run this batch every time I press a button in campaign object, should not be scheduled, but should start every time I click the button. My code currently is the one posted above
    – Toris
    Commented Jun 20, 2022 at 13:16

2 Answers 2


You have the following options available in Salesforce:

There are various variants for each option, it really depends on how you want the user to interact with the server. I apologize for the link-heavy answer, but the main takeaway here is that there are easily half-a-dozen different approaches that you can take that are all valid alternatives. It is up to you to decide which method would be the most appropriate for your situation.

  • 1
    Also: object-specific quick action+screen flow +invocableApex.
    – cropredy
    Commented Jun 20, 2022 at 15:00
  • @cropredy Thanks! I had a feeling I forgot something.
    – sfdcfox
    Commented Jun 20, 2022 at 15:10
  • 1
    If the Quick Action is required in Mobile than Aura is only option as LWC components are not yet supported in Salesforce mobile app.
    – Saroj Bera
    Commented Jun 20, 2022 at 17:20

For this, you can create a quick action button in Campaign Object & call an LWC or an Aura component from it.

And then you can Create an Apex Controller class "ControllerClass" and create a method "callBatch" in that class which will call the batch class.

Then, the call the "callBatch" from Aura or LWC Controller. I hope you know how to call the apex method from Aura or LWC.

To pass the current record id you can pass it from LWC or Aura to the Apex Method "callBatch".

Refer to this for quick action in LWC - Quick Headless Action Refer to this for calling Apex from LWC - Call Apex Methods Imperatively

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .