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
DbjensenDbjensen 

How to Pass Set and Map to Batch Apex class

I have created a batch class. In the Finish method, I am calling another batch class. Can I pass both a set and map to the second batch class? 

This is the first batch class.
  1. public class AddLeadToCallBatchJob implements Database.Batchable<sObject>, Database.Stateful{ 
  2. Set<String> customerPhone = new Set<String>();
  3. Map<Id, String> agentName = new Map<Id, String>(); 
  4. //this is the finish method which I'm passing  in customerPhone   
  5.  public void Finish(Database.BatchableContext bc){
  6.         
  7.         Database.executeBatch(new AddAgentToLeadBatchJob(customerPhone), 100);
  8.     }

Below is the AddAgentToLeadBatchJob class which I am passing in the set 'customerPhone'  from the above class. How can I pass in the map 'agentName' in addition to the set? (If I create a second constructor, I get an error.)
  1. public class AddAgentToLeadBatchJob  implements Database.Batchable<sObject>, Database.Stateful{
  2.  
  3.     Map<Id, String> agentName = new Map<Id, String>();
  4.     Set<String> customerPhone = new Set<String>();
  5.     
  6.     //constructor
  7.     public AddAgentToLeadBatchJob(Set<String> customerPhone){
  8.         this.customerPhone = customerPhone;
  9.     }
Thanks, 
 
Best Answer chosen by Dbjensen
SwethaSwetha (Salesforce Developers) 
HI Dbjensen,

You can try to modify the constructor of AddAgentToLeadBatchJob to take both a Set<String> and a Map<Id, String> as parameters, like this:
 
public class AddAgentToLeadBatchJob implements Database.Batchable<sObject>, Database.Stateful {
    Map<Id, String> agentName = new Map<Id, String>();
    Set<String> customerPhone = new Set<String>();

    // Constructor with both Set and Map parameters
    public AddAgentToLeadBatchJob(Set<String> customerPhone, Map<Id, String> agentName) {
        this.customerPhone = customerPhone;
        this.agentName = agentName;
    }

    // Implementation of Database.Batchable<sObject> and Database.Stateful methods...
}

Then, in the Finish method of AddLeadToCallBatchJob, you can create a new instance of AddAgentToLeadBatchJob with both the customerPhone set and the agentName map as arguments, like this:
public class AddLeadToCallBatchJob implements Database.Batchable<sObject>, Database.Stateful{ 
    Set<String> customerPhone = new Set<String>();
    Map<Id, String> agentName = new Map<Id, String>(); 

    // Implementation of Database.Batchable<sObject> and Database.Stateful methods...

    public void Finish(Database.BatchableContext bc) {
        Database.executeBatch(new AddAgentToLeadBatchJob(customerPhone, agentName), 100);
    }
}

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Dbjensen,

You can try to modify the constructor of AddAgentToLeadBatchJob to take both a Set<String> and a Map<Id, String> as parameters, like this:
 
public class AddAgentToLeadBatchJob implements Database.Batchable<sObject>, Database.Stateful {
    Map<Id, String> agentName = new Map<Id, String>();
    Set<String> customerPhone = new Set<String>();

    // Constructor with both Set and Map parameters
    public AddAgentToLeadBatchJob(Set<String> customerPhone, Map<Id, String> agentName) {
        this.customerPhone = customerPhone;
        this.agentName = agentName;
    }

    // Implementation of Database.Batchable<sObject> and Database.Stateful methods...
}

Then, in the Finish method of AddLeadToCallBatchJob, you can create a new instance of AddAgentToLeadBatchJob with both the customerPhone set and the agentName map as arguments, like this:
public class AddLeadToCallBatchJob implements Database.Batchable<sObject>, Database.Stateful{ 
    Set<String> customerPhone = new Set<String>();
    Map<Id, String> agentName = new Map<Id, String>(); 

    // Implementation of Database.Batchable<sObject> and Database.Stateful methods...

    public void Finish(Database.BatchableContext bc) {
        Database.executeBatch(new AddAgentToLeadBatchJob(customerPhone, agentName), 100);
    }
}

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
DbjensenDbjensen
Hi Swetha  - Thanks so very much for the help. This worked. I appreciate it.