You need to sign in to do that
Don't have an account?
Case Manager
BatchApex - System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
I am making a callout and then trying to insert the data and then immediately I make another callout followed by insert operation. This process continues for users matching my criteria. How to avoid below exception?
I know that its not allowed to do a callout and a DML in series. But as per the requirement I need to follow this process.
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Note: I cannot use future method since I am running this process in a batch class.
I know that its not allowed to do a callout and a DML in series. But as per the requirement I need to follow this process.
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Note: I cannot use future method since I am running this process in a batch class.
What you should do, is find a way where you don't need to do Callout, then DML, then callout. There's likely a way to architect, design around it.
However, if needed, you can chain batch jobs together. You would perform the first callout and DML in one batch, then and then chain a second batch class that performs the second callout. The chain happens by calling the second class in the finish method of the first batch class.
An example might look like the following:
Note, if you need to pass variables between the batch jobs, then you'll want to look into implementing the Database.Stateful interface as well.