3

I'm trying to create a future method that makes a callout, and for some reason it's not invoked when the script is done.

postInstallScript.cls:

global class postInstallScript implements system.InstallHandler {
   global void onInstall(InstallContext ctx) {
      // ...
      myFutureMethod('test');
   }
   @future(callout=true)
   global static void myFutureMethod(string s) {
      // sending email on invoke
   }
}

I'm trying to simply send an email as a test once the method is invoked, and it works only when I take off the @future annotation from the myFutureMethod method, but as the method contains a callout, it cannot be executed from the onInstall method, and trows an error:

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

4
  • How are you testing? Bu writing a test class or actually installing? Mar 19 '19 at 10:44
  • actually installing
    – alechko
    Mar 19 '19 at 10:47
  • Are you doing callout first and then sending email? or vice versa? Mar 19 '19 at 10:48
  • I have an email trigger first thing in the future method, just to indicate it's invoked, and after that I have a callout. but as I've mentioned, the method is not sending the email while having the @future annotation.
    – alechko
    Mar 19 '19 at 10:51
3

Sending Apex Email doesn't instantaneously sends the email. It actually locks the database, and the email will be sent at the end of the transaction. As email has locked the database it actually an uncommitted work pending. No Callout after DML, in our case, no callout after Email.

Solution: Do callout first and then send email.

1
  • Thank you, I've restructured my future method, I took out the emails (I didn't really needed them, they were just for debugging) and moved my DML statement to post-callout position, and now it works as designed. Thanks again.
    – alechko
    Mar 19 '19 at 11:41

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.