Flag Invocable Apex Methods That Make Callouts - Spring '21

Flag Invocable Apex Methods That Make Callouts - Spring '21

In a single transaction, When we have some data processing or email processing happening and on top of it if we have flow configured in such a way to make a callout, we will encounter System.CalloutException.

That is because by the time the callout will be made the changes to the data will be uncommitted and that’s something that the platform doesn’t allow us to do. Hence we get to see the error You have uncommitted work pending. Please commit or rollback before calling out

How to invoke an Apex method from a Flow?

The workaround for this is to pull the callout into a separate transaction using a screen at times this acts as a bottleneck.

Staring spring 21, when we invoke an apex method from the flow, in the @InvocableMethod annotation along with label and description attributes, you can start using a new attribute called callout. We can pass a boolean value to it.

If there exists any logic in the method that is responsible in making a callout, we need to flag it with true(boolean value) and the platform will decide in runtime if the callout has to be performed in a separate transaction or if it has to be accommodated in the same transaction.

Here is some sample code!

@InvocaleMethod(lable='Account Action, 
                description='This method is used to make a callout to external platform', 
                callout='true){
                              
	Http http = new Http();
	
	HttpRequest request = new HttpRequest();
	request.setMethod('GET');
  request.setEndpoint('https://www.salesforcecasts.com');
  
  HttpResponse response = http.send();
  String body = response.getBody();		                
}