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
RohitJRohitJ 

Invoking inter dependent callouts from a managed package

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.
Omar Rajab 94Omar Rajab 94

Hi RohitJ,

create a new Future method, and call the manged package from this method. Put the newFutureMethod in your loop.
Your currentyl future method should not be future anymore!
 

fyi: it is not best practice to perform a method includes DMLs in the for loop!

regards,
Omar Rajab