0

I know as per documentation, we cannot perform DML and then callouts from apex class. In my scenario, I have a callout then I am performing a DML which is placed inside a future method and again there is callout after the call to future method.

Still I am getting "You have uncommitted work pending" error.

Is the solution (apart from changing the order) only to make the callout in future and DML directly? Can't we make DML's in future method to avoid this.

Relevant Code:

global class docuSignConnector {
    public static String callDummy(){
     // Making HTTP Callo
    // making call to future method to insert log object for integration
    createIntegLog.createIntegrationLogFuture(req.getBody(),res.getBody());
    //Making HTTP callouts again - getting mixed DML Error here
   }
}

public class createIntegLog{
  @future
  public static void createIntegrationLogFuture(String reqBody,String resBody) 
  {     
    // Insert the Log Object here with req , response data
  }
}
3
  • 1
    share your relevant code, please Mar 25 '20 at 18:58
  • I have had success creating a platform event with the serialized object that is to be DMLd - then handling the platform event and invoking a callable (hopefully in the same class that invoked the platform event). Mar 25 '20 at 19:26
  • @SebastianKessel I have edited and added the relevant code . Mar 26 '20 at 7:05
4

Enqueuing Asynchronous Apex, including future methods, for execution is a "DML-equivalent" operation. It counts just like DML for purposes of causing an exception in proximity to callouts.

You must call your future method after performing the callout. You can then execute DML in the future method, which executes in a separate transaction context.

2
  • 3
    In some cases, like batch, schedulable, and queueable, it's more than just DML-equivalent. There is also a literal DML operation that is implicitly executed (inserting into AsyncApexJob or CronTrigger, for which we get the Id returned to us when queuing up those jobs).
    – Derek F
    Mar 25 '20 at 19:09
  • @DerekF thanks . So we need to either make all DMLs at the end or make the callouts in future method. Mar 26 '20 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.