0

I need to catch the response of a callout method and insert it into a database. But what the problem is, the callout method called from a trigger.

Therefore, I have to add the annotation @future(Callout=true)

   if( appStatusesBelowDocSent.contains(currentAppStatus) )
   {     
      envelopeId = EsignUtil.getEnvelopeId(app.Id);                                
      Docusign.voidEnvelopeAsync(envelopeId,'Application recalled or status is changed to a previous status');
      // TODO: I need to update a object with the response of Docusign.voidEnvelopeAsync  
   }

I can not insert the data inside the voidEnvelopeAsync in the same transaction with callout.

How can I accomplish that?

  • 1
    What error you are getting? – rahul gawale Apr 19 at 11:33
  • I got no errors. Just want to know, how to deal with this type of requirements – Noor A Shuvo Apr 19 at 11:55
3

You can't get the results of an @future method in the currently running transaction (the transaction from which the asynchronous call is made). The @future method will not even execute until the current transaction completes and commits successfully (i.e., no unhandled exceptions leading to a rollback). There is no workaround; this is an inherent feature of the design of the Salesforce platform.

What you can do is restructure how you are building your functionality to work with the way Salesforce handles asynchronous operations. I'm not familiar with DocuSign, but I'll assume for the sake of illustration that you have access to a Docusign.voidEnvelopeSync() method that does the same callout synchronously.

If that's the case, you can write your own future method that calls that synchronous method and then takes action based upon the result:

@future(callout=true)
public static void doCallout(... parameters ...) {
    // do things ...
    // then:
    if (appStatusesBelowDocSent.contains(currentAppStatus)) {     
        envelopeId = EsignUtil.getEnvelopeId(app.Id);   
        // We're already in an async, callout-permitted environment,
        // so perform the action synchronously here.                             
        Object result = Docusign.voidEnvelopeSync(envelopeId,'Application recalled or status is changed to a previous status'); 

        // Perform DML to update related object.
    }
}
0

I don't think you can do that. Here is a similar question with some workarounds, maybe it will help you.

Wait for Future Method execution before doing DML

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.