I am having a difficult time architecting the flow of declarative work to, and from, apex code.
What I want the flow to be is:
Flow (sends inputs to webservice)
Webservice Callout (Process.Plugin or InvocableMethod)
Flow (takes outputs from webservice)
Because you can not call a webservice from a Trigger, I must make the callout a future method. Because the callout is a future method, you cannot use static variables to return the value from the future method to the synchronous code that called it. Aka I can not pass back output parameters into the flow.
Work Around 1: Create a process builder rather then the trigger to avoid future methods. Because of the order of DML and Callouts, I will always get an "Uncommitted Work Pending" error. The flow will make the DML and Callout loop thus causing DML to happen before and after the callout.
Work Around 2: perform a DML update on the record after making the callout and do not pass back any parameters to the flow. Because of the async/sync differences between the callout, there is a delay between when the future method makes the insert and when the next block of the flow is fired. I need the Object to be inserted before it can move on to the next block in the flow. Due to the nature of the flow, it loops through multiple times thus causing the same error You have uncommitted work pending. Please commit or rollback before calling out
Does anyone have any suggestions (other then platform events) to allow the data to be passed to and from a flow and webservice? I feel like there must be a way to make option #1 work.
Platform Events was the only way for me to accomplish this. I didn't want to have to go this route because I would have to restructure my flow but it ended up being the only solution.
I created a custom Platform Event in SF and updated my webservice code to instantiate the event, and set the field values to the returned callout values. I subscribed to the event in a wait element in the flow and pulled back in the values from the webservice into the flow. I had to put a counter on the event so I was able to track accordingly for when there are multiple events coming in at the same time.
With this solution I was able to go: Apex Trigger -> Pass Input Parameters to Flow -> Call Webservice -> Pass Output Parameters back to Flow.