So In my recent project, I’m working with a Queueable Class where I’m using a For loop to make HTTP rest callout but as the no of callouts increased depends on the size of the data. My code hit the callout governor limit Too many callouts: 101Â so to resolve this I decided to use a batch class as whenever execute method runs it refresh the governor limits within the transaction.
Now the next step is to iterate over a list of requestIds which is a list of strings so you will know how to iterate over a list<sObject> but in this blog post, I will let you know how to pass the list of strings into the execute method.
If you need to iterate over a list<String> Make sure you implement the Database.Batchable<String>Â and if you are making web service callouts within the batch class you also need to implement Database.AllowsCallouts
Learn more about Salesforce Flow Bootcamp
When to use Database.stateful?
Answer by SFDC Fox –
The only time you need a Database.Stateful is when the execute method modifies a class variable in a way meant to be used across multiple execute methods or in the finish method. The majority of batches you will ever write will not need Database.Stateful. It’s important to know that using Database.Stateful will harm your batch’s performance because the class will be serialized at the end of each execute method to update its internal state. This extra serialization results in longer execution time.
If you’re ever not sure if you need it, simply ask yourself two questions: (1) “Does one execute method need data from a previous execute method?”, and (2) “Does the finish method need data from any previous execute method?” If the answer to both of these questions is no, then you do not need Database.Stateful. If the answer is yes, then you may want to use Database.Stateful. Keep in mind that there are alternative means to using Database.Stateful, such as storing data in a Custom Setting, which may offer better performance in some cases.
So in this example, As I’m getting the requestIdList from some another Batch class so I need to create a Constructor of the batch class and In the start method I need to use Iterable<String>Â as return Type and In the execute you need to create a parameter with the same datatype as you are returning in the start method i.e List<String>
Register for Salesforce Flow Bootcamp
For more reference/Information, you can check this out:- Custom Iterators
How Batch Solves the 101 too many Callouts Issue ?
When I’m calling this GetBatchCallout Batch class I’m also passing a custom batch size to less than 100 which means in a single execute method there are always less than 100 callouts which will solve the issue of this governor Limits.
How to run a Batch, class, for less than 100 batch size?
This means I have 99 request Ids per batch that make 99 callouts per batch and hence solve my issue I hope this article helps you in any way.