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);
return;}
Here is sendHttpPostRequest:
@future(callout=true)
public static void sendHttpPostRequest(string url, string postData, Map<string,string> headers)
{
Http h = new Http();
HttpResponse res = new HttpResponse();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('POST');
if(headers != null)
{
for(string key:headers.keySet())
{
req.setHeader(key,headers.get(key));
}
}
req.setTimeout(2000); //2 seconds
req.setBody(postData);
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.startTest();
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
insert sub;
Test.stopTest();
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?