[SalesForce] Help with FLOW/Apex Error: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

I've already looked through all the related posts and can't find a solution for this error:

Error Occurred: An Apex error occurred: System.CalloutException: You
have uncommitted work pending. Please commit or rollback before
calling out

Here's my situation: I have a FLOW that waits for a platform event, pulls the keyword from the event params, if that keyword is null, it calls an @invokableMethod that calls out to an external API.

The docs all say you can't do a callout before the DML actions, but I'm not doing any DML operations at all. I looked through this list that enumerates other operations that might be construed as DML, but I'm not doing any of those either. I also checked the helper methods I'm calling, no or other commands known to throw this error.

Here's my flow and my code:

enter image description here

public class ARpromptKeyword
{
    @InvocableMethod(label='Send AutoResponse Keyword' description='Needs recipientNumber.')
    public static void sendAutoResponseKeyword(List<String> recipientNumber)
    {
        System.debug('sendAutoResponseKeyword called');
        String FromNumber = TwilioAPI.getFromNumber();

        //instantiate the Twilio REST client
        TwilioRestClient client = TwilioAPI.getDefaultClient();

        Map<String, String> properties = new Map<String, String>{
                'To' => recipientNumber[0],
                'From' => FromNumber,
                'Body' => 'Please enter SERVICE or SALES to chat with a team member.'
        };
        TwilioMessage message = client.getAccount().getMessages().create(properties);
    }
}

Any ideas?


I went back and minimized my flow with no data passing or saving, even to internal flow variables. Nothing but waiting for the platform event, then calling the invokable method with no parameters. I still get the same error.

enter image description here

Now, I'm going to try to think of some simpler invokable method to try as my next test…


Further testing tells me calling an Apex Invokable Method works for something like saving a Contact. It appears that it's the last line of the code in my method, where it does the callout, that the problem occurs.

Best Answer

Found the answer, I think, or at least a reasonable workaround. I remembered that Triggers can't make callouts -- they can only call an @future method to do so. I wondered if Flows are the same, so I tried the following:

Flow calls an @InvokableMethod, which in turn, calls an @future method to make the callout. It works!

 public class FlowARpromptKeyword
{
    @InvocableMethod(label='Send AutoResponse Keyword' description='Needs recipientNumber.')
    public static void callARpromptKeyword(List<string> recipientNumber)
    {
        ARpromptKeyword.sendAutoResponseKeyword(recipientNumber[0]);
    }
}

And...

public class ARpromptKeyword
{
    @future(callout=true)
    public static void sendAutoResponseKeyword(String recipientNumber)
    {
        String FromNumber = TwilioAPI.getFromNumber();

        //instantiate the Twilio REST client
        TwilioRestClient client = TwilioAPI.getDefaultClient();

        Map<String, String> properties = new Map<String, String>{
                'To' => recipientNumber,
                'From' => FromNumber,
                'Body' => 'Please enter SERVICE or SALES to chat with a team member.'
        };
        TwilioMessage message = client.getAccount().getMessages().create(properties);
    }
}
Related Topic