# Issues accessing already Created Process Instance

**URL:** <https://forum.camunda.io/t/issues-accessing-already-created-process-instance/48600>\
**Category:** Camunda 7 Topics\
**Tags:** process-engine\
**Created:** [November 2, 2023, 10:42pm UTC](https://forum.camunda.io/t/issues-accessing-already-created-process-instance/48600 "2023-11-02T22:42:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madan\_Kumar\_Maha](https://forum.camunda.io/user_avatar/forum.camunda.io/madan_kumar_maha/32/27382_2.png) [@Madan\_Kumar\_Maha](https://forum.camunda.io/u/Madan_Kumar_Maha)\
**Post date:** [November 2, 2023, 10:42pm UTC](https://forum.camunda.io/t/issues-accessing-already-created-process-instance/48600/1 "2023-11-02T22:42:08Z")

</div>

We are having issues with Camunda when trying to use an already created Process Instance , where we are running into some sort of race conditions where we are not able to Query the already created process instance for a particular businesskey.  
The **ProcessInstance** ( Variablename - 'existingProcess; in below code) that was created for a quote returns null and we end up creating a new process instance through the correlate method.  
We are expecting the existingProcess(ProcessInstance) to return a value if the processInstance has already been created for that particular businesskey(quotenumber below) . This creates duplicate process instance for the same quoteNumber.

Sample Code:

import org.camunda.bpm.engine.RuntimeService;  
import org.camunda.bpm.engine.delegate.DelegateExecution;  
import org.camunda.bpm.engine.delegate.JavaDelegate;  
import org.camunda.bpm.engine.runtime.ProcessInstance;  
import org.springframework.stereotype.Component;

import java.util.Collection;  
import java.util.Objects;  
import java.util.Set;  
import java.util.stream.Collectors;

@Component  
@Slf4j  
public class ProcessQuoteDelegate implements JavaDelegate {

```
@Override
public synchronized void execute(DelegateExecution execution) throws Exception {
    ProjectDto project = (ProjectDto) execution.getVariable("project");
    String projectNumber = (String) execution.getVariable("projectNumber");
    String quoteNumber = (String) execution.getVariable("quoteNumber");

    Set<String> lineItemMasterIds = project.getPurchaseOrders().stream()
            .map(PurchaseOrderDto::getLineItemMasters)
            .flatMap(Collection::stream)
            .filter(lineItemMaster -> Objects.equals(lineItemMaster.getQuoteNumber(), quoteNumber))
            .map(LineItemMasterDto::getLineItemMasterId)
            .collect(Collectors.toSet());

    RuntimeService runtimeService = execution.getProcessEngine().getRuntimeService();
    ProcessInstance existingProcess = runtimeService.createProcessInstanceQuery()
            .processDefinitionKey("process-quote")
            .processInstanceBusinessKey(quoteNumber)
            .singleResult();

    if (existingProcess == null) {
        log.info("No existing process instances found for quote {}, creating new process instance", quoteNumber);

        runtimeService.createMessageCorrelation("LineItemMastersReceivedEvent")
                .processInstanceBusinessKey(quoteNumber)
                .setVariable("projectNumber", projectNumber)
                .setVariable("quoteNumber", quoteNumber)
                .setVariable("lineItemMasterIds", lineItemMasterIds)
                .correlate();
    } else {
        log.info("Merging new line item masters for quote {} into existing process instance {}", quoteNumber, existingProcess.getProcessInstanceId());

        runtimeService.createMessageCorrelation("AdditionalLineItemMastersReceivedEvent")
                .processInstanceId(existingProcess.getId())
                .setVariableLocal("newLineItemMasterIds", lineItemMasterIds)
                .correlate();
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![StephanHaarmann](https://forum.camunda.io/user_avatar/forum.camunda.io/stephanhaarmann/32/21924_2.png) [@StephanHaarmann](https://forum.camunda.io/u/StephanHaarmann)\
**Post date:** [November 3, 2023, 6:54am UTC](https://forum.camunda.io/t/issues-accessing-already-created-process-instance/48600/2 "2023-11-03T06:54:26Z")

</div>

Hello @Madan_Kumar_Maha and welcome to the forum,

To query the process instance via the runtime service, the instance must be written to the database.  
By default, Camunda 7 commits an instance to the database when it reaches a wait state. You can read more about [process instances](https://docs.camunda.org/manual/7.20/user-guide/process-engine/process-engine-concepts/#process-instances) and [wait states](https://docs.camunda.io/docs/components/best-practices/development/understanding-transaction-handling-c7/) in the documentation.  
In your case, you should set the async before flag on your process start event:

 ![image](https://forum.camunda.io/uploads/default/original/3X/f/8/f8239ad3e23f22b9e86c45f00a31b94c14311c8c.png)

I hope this helps.

Stephan
