Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DML transactions get interrupted by endpoint calls #8

Closed
ArturMoczulski opened this issue Jun 24, 2019 · 1 comment
Closed

DML transactions get interrupted by endpoint calls #8

ArturMoczulski opened this issue Jun 24, 2019 · 1 comment

Comments

@ArturMoczulski
Copy link
Contributor

Feedback from a user:

If I have code like this, where I perform a DML operation first then try to Rollbar:

upsert someRecord;
Rollbar.init();
Rollbar.log('error', 'API Check Failed');

I then do indeed seeing this error:

17:46:30.0 (610688168)|FATAL_ERROR|System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

Class.Notifier.send: line 45, column 1
Class.Notifier.send: line 34, column 1
Class.Notifier.log: line 14, column 1
Class.Rollbar.log: line 41, column 1

So one thing I tried is a thin wrapper over Rollbar:

public class RollbarClient {
    private static final String ERROR_LEVEL = 'error';
    private static final String WARN_LEVEL = 'warning';

    @future(callout=true)
    public static void error(String message) {
        log(ERROR_LEVEL, message);
    }
    public static HttpResponse errorSync(String message) {
        return log(ERROR_LEVEL, message);
    }

    public static HttpResponse log(String level, String message) {
        Rollbar.init();
        return Rollbar.log(level, message);
    }
}


Using the @future decorators. This works in some cases, but then I found this case:

if(some condition) {
    RollbarClient.error('error', 'API Check Failed');
    throw new AuraHandledException('Some error');
}

It turns out if you throw in an Apex transaction, all pending work is rolled back, including future methods, so the future methods are never "saved" to be executed later.

So I added an errorSync and warnSync method to this class to use in cases where an exception is thrown in the same transaction.
@waltjones
Copy link
Contributor

#16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants