I am creating a very basic flow. It is set that when record is updated and 1 specific condition is met that it will call an apex action that will call an api. I have set it up and the code is below but every time I meet the condition it fails with the following error: An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
I have done some reading and I see I can't do a DML action and then call a web service, but I am not sure how I can fix this in this scenario.
I did not include the beginning part of the SendToAPI class as it contains the API key, but it just sends the key with the body and it's a post call.
public class APIController {
@invocablemethod(Label='Call API' description='send to external system')
public static void callMySystem() {
SendToAPI.MySystemCall();
}
}
public class SendToAPI {
public static void MySystemCall() {
HttpResponse response = http.send(request);
// Parse the JSON response
if(response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' + response.getStatusCode()
+ ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}