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
Sandrine94748Sandrine94748 

calling webservice in future handler from a trigger | Error | System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

Hello,

I have following error:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

My usecase:
I am calling a webservice in a future handler. 

Other workaround i tried:
I tried to call the webservice without a future handler, but as i am calling in trigger it is giving me error.

Can someone please tell me any workaroud ?

thank you
Best Answer chosen by Sandrine94748
mukesh guptamukesh gupta
Hi Kiran,

Please follow below code:- 
trigger descriptionUpdater on Account (after insert) {

  System.debug('Making future call to update account');
  for (Account acc : Trigger.New) {
    //Call future method to update account
    //with data from external server.
    //This is a async calls, it returns right away, after
    //enqueuing the request.

    AccountUpdater.updateAccount(acc.Id, acc.Name);
  }

}
 
public class AccountUpdater {

  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateAccount(String id, String name) {

    //construct an HTTP request
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://cheenath.com/tutorial/sfdc/sample1/data.txt');
    req.setMethod('GET');

    //send the request
    Http http = new Http();
    HttpResponse res = http.send(req);

    //check the response
    if (res.getStatusCode() == 200) {

      //update account
      Account acc = new Account(Id=id);
      acc.Description = res.getBody();
      update acc;
    } else {
      System.debug('Callout failed: ' + res);
    } 
  }
}

Kindly mark my solution as the best answer if it helps you.


Regards
Mukesh
 

All Answers

mukesh guptamukesh gupta
Hi Kiran,

Please follow below code:- 
trigger descriptionUpdater on Account (after insert) {

  System.debug('Making future call to update account');
  for (Account acc : Trigger.New) {
    //Call future method to update account
    //with data from external server.
    //This is a async calls, it returns right away, after
    //enqueuing the request.

    AccountUpdater.updateAccount(acc.Id, acc.Name);
  }

}
 
public class AccountUpdater {

  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateAccount(String id, String name) {

    //construct an HTTP request
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://cheenath.com/tutorial/sfdc/sample1/data.txt');
    req.setMethod('GET');

    //send the request
    Http http = new Http();
    HttpResponse res = http.send(req);

    //check the response
    if (res.getStatusCode() == 200) {

      //update account
      Account acc = new Account(Id=id);
      acc.Description = res.getBody();
      update acc;
    } else {
      System.debug('Callout failed: ' + res);
    } 
  }
}

Kindly mark my solution as the best answer if it helps you.


Regards
Mukesh
 
This was selected as the best answer
Satyajeet MaharanaSatyajeet Maharana
Hi Kiran,

Could you please check and confirm if there any DML statements before your callout? You would need to put all DML statements after your callout or you would need to do the callout in a different context altogether. 

Please see this help article for resolution : https://help.salesforce.com/articleView?id=000079772&type=1

Thanks!
Satyajeet
v varaprasadv varaprasad
Hi Kiran,
More info on this issue check below links : 

https://help.salesforce.com/articleView?id=000003701&type=1
https://help.salesforce.com/articleView?id=000079772&type=1
https://salesforce.stackexchange.com/questions/115434/how-to-solve-callout-error-you-have-uncommitted-work-pending-please-commit-or


If you have still had issue please provide your sample code.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Gautam Manchanda 12Gautam Manchanda 12
Hi Kiran,

This exception occurs when a record is in transaction of some DML and you try to make a callout using the same record.

Since your record is not commited to the database still, you will not be able to process the record asynchronously(future callout). Your trigger would be still processing the record and in between you would have invoked the future method and this leads you to face the exception.

You can place the future call at the end or try creating a @invocable function and later process the webservice callout through process builder while all your triggers would have successfully executed by that time!

Thanks. 
Sandrine94748Sandrine94748

Hello All,
thank you for suggestion.
Just to give more information, there are around 10 trigger and few process builder on the Asset object.
It is not possible to paste the code as its too big and security concerns.
I totally agree with the exception. but i am finding a workaround which will not affect the exsisting functionalities.
I will try to implement the Gautams idea.

udhay SFDCudhay SFDC
Hi All, 

in my case, Trigger webservice (in future handler ) and updating Asset object. 
Working for few records, when we do bulk load, I am running into same issue. 

I struck with same issue for a long time,  did you get any solution for this?