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

String Batch Job (string job not receiving all set items)

I have created a batch class. In the Finish method, I am calling another batch class. I am passing the phone numbers from the first class to the second class using a a set called 'customerPhone '. From what I'm seeing, the second class is not receiving the full list of phone numbers.  

This is the first batch class.

//this set is populated from the execute method. I can see in the logs all phone numbers in the set.
  1. Set<String> customerPhone = new Set<String>();
  3. //this is the finish method which I'm passing  in customerPhone   
  4.  public void Finish(Database.BatchableContext bc){
  6.         Database.executeBatch(new AddAgentToLeadBatchJob(customerPhone), 200);
  7.     }
Below is the AddAgentToLeadBatchJob class which I am passing in the set 'customerPhone'  from the above class. 
  1.     Set<String> customerPhone = new Set<String>();
  3.     //Query leads with matching phone numbers from tethr event
  4.     public Database.QueryLocator start(Database.BatchableContext batchContext){
  6.         return Database.getQueryLocator( [SELECT Home_Phone_Removed_Special_Characters__c, Email, Tethr_Agent__c   FROM Lead WHERE Home_Phone_Removed_Special_Characters__c  IN :customerPhone ORDER BY CreatedDate ASC]);        
  7.     }
  9. public void execute(Database.BatchableContext batchContext, List<Lead> eventQuery){
  11.             System.debug('customerPhone in execute...' +customerPhone.size());                                                      
  13.         }
In my test, I am updating 299 records in the first class and passing those phone numbers to the second class. The second class should find 299 matching records. Howevever, the logs show only 99 records are passed to the second batch class. 

Any help would be greatly appreciated. 

SubratSubrat (Salesforce Developers) 
Hello ,

You mentioned that you can see all phone numbers in the set in the logs, but it's important to confirm that the set is actually being populated correctly. You can add a System.debug statement in the execute method of the first batch class to check if the phone numbers are being added to the set as expected.

Check if there are any duplicates in the set of phone numbers. If there are duplicates, the second batch class may not receive all the phone numbers that were passed from the first batch class. You can use the size() method to check the size of the set after removing duplicates.

For Example :
Set<String> uniquePhoneNumbers = new Set<String>(customerPhone);
System.debug('Size of unique phone numbers set: ' + uniquePhoneNumbers.size());
Check if the second batch class is processing all the records it receives. In the execute method of the second batch class, you can add a System.debug statement to check if all the records are being processed:
System.debug('Number of records received by second batch class: ' + eventQuery.size());

Hope the above information helps !
Thank you.
Hi Subrat - Thanks for the response. I verified that all of the phone numbers are unique (no dupes). Here is what I am seeing in the logs. 

1. In the execute method, the customerPhone set is 200 (red X in pic below)
09:19:11.0 (23477330)|USER_DEBUG|[35]|DEBUG|size of customerPhone set... 200

2. Then the second batch runs.

3. In the finish method, the size of the customerPhone set is 99 (blue X in pic)
09:19:12.0 (12945304)|USER_DEBUG|[50]|DEBUG|size of customerPhone in finish statement... 99

4. This is the start method the second batch job which shows the # of records passed in from the first batch class: (red circle in pic) 
09:19:12.0 (14221841)|USER_DEBUG|[25]|DEBUG|customerPhone in start...99

You'll notice there is no debug statement for the first 200 batch of records. The logs above the red circle are just updates to the second batch class of 99 records. Would it be helpful for you to see the entire code - it's fairly small? 

User-added image 
I ended up just puting 2 queries in the start method rather than separating them into different classes. This resolved the issue.