I am trying to execute a web service call to our backend system when the opportunity is closed/won. I am attempting to do a POST to our endpoint. All the api call is really carrying the account name and a few structural detail back (All listed below). My problem is that when the trigger executes I get the following error in the debug logs "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out".
The Process Builder Passing in the company name:
APEX Class
public class Play2 {
@TestVisible static HttpResponse res;
@InvocableMethod
public static void sendNotification(List<String> name) {
HttpRequest req = new HttpRequest();
Http http = new Http();
String payload = JSON.serialize(
new Map<String, String> {
'companyName' => name.get(0),
'salesforceAccountType' => 'Company',
'salesforceSynchStatus' => 'Enabled'
}
);
req.setEndpoint('https://TheEndPoint');
req.setMethod('POST');
req.setHeader('Authorization', 'The Mighty API KEY');
req.setHeader('Content-Type', 'application/json');
req.setBody( payload );
req.setCompressed(true);
try {
System.debug('Point 1');
res = http.send(req);
System.debug('Point 2');
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(res.toString());
}
}
}