0

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.

1 Answer 1

1

You can create class that implements Queueable and make the last callout in it. So the final logic will be:

  • Scheduler: serialize data for callout in future method
  • Future(Callout=true): run first callout group, save data from responses (which is needed) for queue
  • Queueable class implements System.Queueable, Database.AllowsCallouts: run second callout group, make DML
2
  • 1
    Hi @Mariia, Thank you for your answer. I have already implemented this using the similar approach which you have mentioned and it is working well.
    – Rohit
    Sep 15, 2021 at 14:11
  • if that is the case, then please make sure you mark this as the best answer.
    – glls
    Feb 3, 2022 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .