3

I'm trying to get some advanced architecture setup for my company, but I'm running into an issue with my design; looking for help.

I'm attempting to execute an API call from Flow, but I'm getting the dreaded CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.. I know this means I need to put @Future on my call out, but I'm wondering if anyone knows of a way to put that function into the flow? Is there any element that would commit the work of my transaction?

The main reason I ask is I want to get the response of the call out into the flow - meaning I cannot use the future annotation as it prevents the response from being returned. If nothing else works I'm going to have to leverage the response (as an inserted record) to trigger a follow up flow, but this is a heavier design than I'd like and creates some security considerations.

I've attempted using a wait element, but it appears the method invoked is still considered part of the same transaction. I setup an absolute alert for 0.016777 hours (1 minute) after current time, but still receiving the error. I'm going to try a relative alert to see if that works.

TL;DR: I'm looking for a way to get Flow to commit work prior to a specific element. Is there a way to do this?

6
  • Does your flow have any screen elements? If so a screen should start a new transaction.
    – gorav
    Commented Feb 1, 2017 at 21:42
  • Also have you seen the spring17 external services beta? The example in the release notes seems like it's similar to your use case. releasenotes.docs.salesforce.com/en-us/spring17/release-notes/…
    – gorav
    Commented Feb 1, 2017 at 21:49
  • @gorav unfortunately, no screen elements - we want the entire process automated, so no user interactions. Thanks for the external services link. I'm not up-to-date on that feature, is that a beta for standard Salesforce or is one of their lightning connect services required?
    – zainogj
    Commented Feb 1, 2017 at 22:01
  • Not sure, just have it tucked away to explore once we get spring.17, kinda seems like it's standard but few things are these days. Will see if I can find docs on it.
    – gorav
    Commented Feb 1, 2017 at 22:28
  • cant we do sync callout instead of future callout, I have similar req but need to do Sync callout as I need response come into flow. please advise. Commented Feb 14, 2017 at 20:29

2 Answers 2

3

There is nothing that can explicitly cause a commit in a way that gets rid of this error in live code. System.runAs prevents this error, but only for unit tests (because System.runAs only works in unit tests). You can use an InvocableMethod in a flow, and from there, kick off a future method, thereby splitting the transaction in two; the future method can safely perform the callout after the flow's current transaction completes and is committed.

public class DoCalloutFromFlow {
    @InvocableMethod(label='Do Callout', description='Performs a callout after the flow completes')
    public static void doCallout(Id[] params) {
        futureCallout(params);
    }
    @future(callout=true)
    public static void futureCallout(Id[] params) {
        // Do your callouts here
    }
}

Note: You can actually cause commits during a Visual Flow, as each screen element ends a transaction. However, when you're using any sort of Process Flow, you're already in DML-land and can't perform a callout.

3
  • Thanks @sfdcfox, that was my fear. Sounds like I'll have to redesign this to go into @future then store my response and trigger a follow-up flow to get what I want. lol at DML-land.
    – zainogj
    Commented Feb 1, 2017 at 15:47
  • @sfdcfox are you familiar w the flow external services beta? I haven't tested it yet, not sure about cost either. It seems to address something along these lines releasenotes.docs.salesforce.com/en-us/spring17/release-notes/…
    – gorav
    Commented Feb 1, 2017 at 22:37
  • @gorav That's cool stuff. Not sure if it would work anywhere, or just in Visual Flow, but it looks handy.
    – sfdcfox
    Commented Feb 1, 2017 at 23:16
0

You can force a flow to commit changes by pausing it, using a Pause flow element:

Transactions and Paused Interviews

A transaction ends as soon as a flow interview pauses for one or more resume events. When the flow interview resumes, a new transaction begins. Everything after the Pause element is executed as part of a batch transaction that includes other resumed interviews.

From https://help.salesforce.com/s/articleView?id=sf.flow_considerations_design_pause.htm&type=5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .