2

I want to perform 5 callouts and log any callout exceptions as a new case. Since mixing callouts and DMLs is not allowed, I made the logging asynchronous. Unfortunately, an ExecutionException ("uncommitted work pending") is still being thrown when the new case is inserted into the DB by the @future method. Here's the callout class (simplified):

class CalloutClass {
    // ...
    public CustomResponse doCallout() {
        try {
            // ... callout logic
        } catch (CalloutException coe) {
            LogUtil.logException(coe.getTypeName());    // @future method
        }
    }
}

Are there any workarounds for this problem?

5

Create a List of Cases that you want to insert once all the callouts are complete. Each time time you catch a CalloutException add the new case to this list.

Once you have completed (or attempted) all your callouts insert the list of cases.

class CalloutClass {

    private List<Case> CalloutExceptions { get; set; }

    public CalloutClass() {
        this.CalloutExceptions = new List<Case>();
    }       

    public CustomResponse doCallout() {
        try {
            // ... callout logic
        } catch (CalloutException coe) {
            // No longer future. Just returns the Case to insert.
            Case caseToInsert = LogUtil.logException(coe.getTypeName());    
            this.CalloutExceptions.add(caseToInsert);
        }
    }

    public void logCalloutCases() {
        if(this.CalloutExceptions.size() > 0) {
            insert this.CalloutExceptions;
        }
    }
}

CalloutClass cc = new CalloutClass();
cc.doCallout();
//... Make the remainder of the callouts.
cc.logCalloutCases();

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.