I have a requirement, where I have a scheduled apex which runs every 5 mins and invokes a future method of an apex class. This future method invokes a managed package method for making the callout. So the current logic is that in a for loop for every record I am forming the request and invoking the Managed package method to perform the callout. The callout gives a response and on the basis of response I have to perform another callout or just move ahead. Now the problem is that there is DML transaction happening in the Managed package method as confirmed by the client. So while performing the 2nd callout or the callout for next record, I get the System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.
What approach can I take achieve this requirement?
Here is the snippet of how logic is written now
Schedule Class -> invokes Apex class method processRecords() { In this there are 3 SObjects maps which are serialized and sent to the future method }
futureMethod(){ Deserialize the input
for(every record)
{
create the request
invoke the managed package method to perform callout
if response success put data in map for DML in the end after for
loop
if response failure invoke 2nd callout
}
}
In the end perform DML operations.