[SalesForce] Batch Apex that does DML and multiple callouts

I need to write a Batch Apex that does DML and multiple callouts at different points in the process, then chains to another batch that also does DML and multiple callouts. Here's how I plan to do it, but is there a step where I might run into "uncommitted work pending" or "Callout from scheduled Apex not supported" problems?

  1. The batch will be Stateful and AllowsCallouts.

  2. In the start method, check if an Oauth2 token refresh is needed. If so, make a callout to get the new token, then save the new token to custom settings.

  3. In the execute method, make a callout to create an object in the external system. I may need to create multiple objects of this type, so I will set the batch scope to 1. Place the object ID in a stateful collection. Don't do any DML.

  4. In the finish method, write the external system object IDs to a Salesforce custom object and update a timestamp in custom settings, and chain to another batch file. This second batch file will do the same steps for a different external object.

  5. In particular I'm wondering if a chained batch is considered "scheduled Apex" because that would break the second batch's functionality.

Do you think this should work?

Best Answer

You can combine the callout and the DML in the same method if you want to; the only restriction is that no callouts are allowed after a DML. Each call to start, execute, and finish are separate transactions. There's really no need to perform your DML in the finish method, as you can do so in the execute method. Notably, if you're trying to do more than 10,000 callouts before you finally write to the database, you'll exceed the DML limit.

Your current design would certainly work, assuming the OAuth token doesn't expire until the end of the batch. Personally, I'd recommend moving the OAuth check into the execute method so that if you lose your token half-way through (say, because it's revoked), your batch can recover. You may also want to increase your scope size from 1 to a larger number, depending on how much callout time you think you'll need.

To calculate how many callouts you can do, figure out how much time is needed for each callout, and divide that into the maximum callout time. For example, if your callout takes an average of 5 seconds, then your limit would be 60 callouts in a transaction. Of course, you can't go over the governor limit of 100, so that'd be your maximum value.

Finally, batchable Apex isn't scheduled Apex, even though both are a form of "asynchronous" Apex. You won't need to worry about that error, even if you're chaining, and even if you use scheduleBatch to insert a delay inbetween.

Related Topic