0

i cannot invoke asynchornous method before Callout,how to resolve this issues?

public class CalloutService {
    public void callout_test1() {
        CalloutService.doDML();
        HttpResponse r = call();
        //process ...

    }
    public void callout_test2() {
        BatchUpsert bu = new BatchUpsert();
        Database.executeBatch(bu);
        HttpResponse r = call();
        //process ...

    }
    public HttpResponse call() {
        Http h = new Http();
        HttpRequest r = new HttpRequest();
        r.setEndpoint('https://ap2.salesforce.com');
        r.setMethod('GET');
        HttpResponse r2 = h.send(r);
        return r2;
    }

    @future()
    public static void doDML() {
        //
        //Account a = [SELECT Id, Name FROM Account LIMIT 1];
        //UPDATE a;
        System.debug('----');
    }
}

when i use it as follow

CalloutService cs = new CalloutService();  
cs.callout_test1();  

Or like this:

CalloutService cs = new CalloutService();  
  cs.callout_test2  

Then i got this problem(even my future or batch method has no dml operations):

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

pls help me out.

2

As per Salesforce documentation below are possible workarounds.

https://help.salesforce.com/apex/HTViewSolution?id=000079772&language=en_US

Possible workaround :-

1) You either need to commit the transaction, make the callout prior to any database changes or move your callout to an @future method (place @future annotation to the webservice method).

2) Splits the transaction into two separate Ajax processes. The first inserts the record and the second performs the callout and is able to update the newly inserted record.

2)You can save the records and then respond to the user with a temporary page with a "Loading" message as you make the second call back to perform the callout. To make it look seamless, you can also make successive AJAX calls to save records and to make callout as user sees "Loading" message upon clicking "Save" button.

4) Sometimes customer do not want to place @future annotation to the webservice method as they need a response from the webservice to decide if they have to rollback the insert or not ?

They can execute an action to insert the object and then execute the webservice callout on the oncomplete event of a commandButton. Then, return a PageReference giving the user immediate feedback. If the webservice callout returns any error, then delete the object and returning the user back to the same page.

Another example provided by Salesforce: https://help.salesforce.com/apex/HTViewSolution?id=000003701

  • No,i need do some dml operations before executing callout not in controller,so i need manually to separate its to two transactions,does batch or future method is belong another transaction? i am so confused,if not,have there any path to meets my needs? – F.Jony Jun 24 '16 at 7:18
  • Can't you able to do normal dml and callout in future method? – Saroj Bera Jun 24 '16 at 7:24
  • i need get callout result to do next process,it's synchornous, so callout method cannot be defined as future method. – F.Jony Jun 24 '16 at 7:32
  • You can always do dml after webservice callout. Can you please explain what is the order you want to execute? – Saroj Bera Jun 24 '16 at 9:45

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.