Fluent API: BpmnModelException: Unable to determine an unique previous gateway

Hi,

I am trying to create a BPMN file using fluent API and had hit an issue. This is pretty similar to the invoice approval example in github

The issue seems to be that we cannot connectTo to an elementId that we can already connected in a previous moveToLastGateway.

I had created the similar model via the Camunda Modeler and it works fine.
This is the code I am trying to build

    BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("invoiceApproval")
      .name("Invoice Approval Process")
      .startEvent()
        .name("Invoice Created")
      .userTask()
        .id("accountantReview")
        .name("Accountant Review")
        .camundaAssignee("accountant")
      .userTask()
        .name("Approver Level 1 Approval")
        .camundaAssignee("approvalLevel1")
      .exclusiveGateway()
        .id("approve1")
        .name("Approval 1")
        .gatewayDirection(GatewayDirection.Diverging)
      .condition("Approved", "#{approved}")
      .userTask()
        .name("Approver Level 2 Approval")
        .camundaCandidateGroups("approvalLevel2")
      .exclusiveGateway()
        .id("approve2")
        .name("Approval 2")
        .gatewayDirection(GatewayDirection.Diverging)
        .condition("Approved", "#{approved}")
      .endEvent()
        .name("Invoice Fully Approved")
      .moveToLastGateway()
        .condition("Return", "#{not approved}")
        .connectTo("accountantReview")
      .moveToLastGateway()
        .condition("Return", "#{not approved}")
        .connectTo("accountantReview")
      .done();

The exception is:

org.camunda.bpm.model.bpmn.BpmnModelException: Unable to determine an unique previous gateway of accountantReview

	at org.camunda.bpm.model.bpmn.builder.AbstractFlowNodeBuilder.findLastGateway(AbstractFlowNodeBuilder.java:287)
	at org.camunda.bpm.model.bpmn.builder.AbstractFlowNodeBuilder.moveToLastGateway(AbstractFlowNodeBuilder.java:294)
	at org.camunda.bpm.quickstart.CreateInvoiceProcessTest.testCreateInvoiceProcess(CreateInvoiceProcessTest.java:70)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
	at org.camunda.bpm.engine.test.ProcessEngineRule$1.evaluate(ProcessEngineRule.java:170)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.camunda.bpm.model.bpmn.BpmnModelException: Collection expected to have <1> entry but has <2>
	at org.camunda.bpm.model.bpmn.impl.QueryImpl.singleResult(QueryImpl.java:69)
	at org.camunda.bpm.model.bpmn.builder.AbstractFlowNodeBuilder.findLastGateway(AbstractFlowNodeBuilder.java:282)
	... 27 more

@Bejoyzm after endevent there’s a duplication of .moveToLastGateway with same condition. Do you really need it?

Hi,

I have two exclusive gateway that on “return” needs to go to Account Review. What would be the best way to do that?

This is model that I have created via the camunda modeler:
image