I'm building an application which sends a post request to an endpoint when a particular record is inserted.

The trigger looks like this(for testing purposes):

trigger NotifyPlayersOfShowCancelation on Submittal__c (before insert) {
HttpUtility.sendHttpPostRequest('test.com','some data', null);

Here is sendHttpPostRequest:

public static void sendHttpPostRequest(string url, string postData, Map<string,string> headers)
    Http h = new Http();
    HttpResponse res = new HttpResponse();
    HttpRequest req = new HttpRequest();
    if(headers != null)
        for(string key:headers.keySet())
    req.setTimeout(2000); //2 seconds
    res = h.send(req);
    system.debug('sent post request successfully');

Now let's get to the weird part. every time I try and test the trigger with a test class:

    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
    insert sub;

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

From my research, it appears this error is thrown when you try and do a DML action and then do a callout afterwards.

When I copy and paste the trigger onto a different object and test that, it completes perfectly fine.

I've removed every trigger for Submittal__c besides my test one, yet the error persists. I do have workflow actions for Submittal__c that changes certain fields on insert, but according to this:


The trigger should execute way before it gets to the workflow rules. Any suggestions?


For future visitors:

It was in fact the workflow updates that were making the error occur.

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.