Below is my controller
public class FlowController {
public String flowResult {get; set;}
Map<String, Object> inputs = new Map<String, Object>();
public Integer ICP_Count {get; set;}
public String dbHtml {get; set;}
Flow.Interview.Flow_ICP_Component_Update_Workflow myFlow =
new Flow.Interview.Flow_ICP_Component_Update_Workflow (inputs);
public void start() {
myFlow.start();
ICP_Count = Integer.valueOf(myFlow.getVariableValue('ICP_Count'));
flowResult = 'Flow Completed';
}
}
Visualforce page
<apex:page controller="FlowController">
<apex:pageBlock title="ICP Component Flows">
<apex:form>
<apex:commandButton action="{!start}" value="UpdateComponentWorkflow" reRender="text"/>
<br></br>
<apex:outputLabel id="text">Total ICPs Processed = {!ICP_Count}</apex:outputLabel>
<br></br>
<apex:outputLabel id="text3">FlowResult = {!flowResult}</apex:outputLabel>
</apex:form>
</apex:pageBlock>
</apex:page>
When I click the "UpdateComponentWorkflow" button for the first time the flow seems to run fine and updates the "Total ICPs Processed" field. But, it doesn't update the "FlowResult". And when I click the button again I get the following error
Interview already started.
Error is in expression '{!start}' in component <apex:commandButton> in page flow_jobs: Class.Flow.Interview.start: line 51, column 1
Class.FlowController.start: line 15, column 1
An unexpected error has occurred. Your solution provider has been notified. (Flow)
To get the button working again I have to refresh the page. My assumption is the flow (autolaunched) is not ending properly or something to do with the lifecycle of the VF page. I would like some guidance on this.
Edit: I thought it would be helpful if I showed the flow diagram
The problem is that you are creating a singleton instance of the Flow which you start the first time you click the button. Then, when you click the button again, you are calling start() on the same instance of the Flow.
The way to resolve this would be to instantiate the Flow every time you click the button, like so:
public class FlowController {
Flow.Interview.Flow_ICP_Component_Update_Workflow myFlow;
Map<String, Object> inputs = new Map<String, Object>();
public void start() {
myFlow = new Flow.Interview.Flow_ICP_Component_Update_Workflow(inputs);
myFlow.start();
}
}