I’m currently trying to find out if there is an simple way to retrieve all process variables for each process instance that has been completed or is currently active for a particular process definition. I understand I can get the process instance IDs with the following two Java API calls:
// Get active process instances for process definition "requestProcess"
List<ProcessInstance> requests =
runtimeService.createProcessInstanceQuery().processDefinitionKey("requestProcess").list();
// Get completed process instances for process definition "requestProcess"
List<HistoricProcessInstance> variables =
historyService
.createHistoricProcessInstanceQuery()
.processDefinitionKey("requestProcess")
.list();
Unfortunately, I’m not able to access the process variables for each of those process instances. Is there a way I can easily retrieve the process variables for each process instance?
In my use case, I’m currently storing the data related to my requests in the process variables. I’m also wondering if I should be storing the request business data in Camunda? Or should I be storing this in a DB and then referencing the DB data via processInstanceId or business key?
Good question - first I want to clarify what kind of data is stored in the Runtime and History database.
Runtime contains all active processes instances.
History contains all finished process instances as well as as all active process instances.
(NOTE: this is if you’re using the default history settings)
This just means that you shouldn’t need to query both sets of tables, you should be able to get everything you need from the history database.
if you want to access the variables for a given list of processes instances you can use this
historyService.createHistoricVariableInstanceQuery()
.processInstanceIdIn("List of process instance ids")
.list();
It’s generally a bad idea to store things in the engine that aren’t directly needed to execute the process. It’s often much, much easier in the long run to store business data independently and just store a key for that data in the process engine so that you can combine them later.
Cool, thanks for the clarification @Niall. I’ll look into createHistoricVariableInstanceQuery() method as I pass in my process instance IDs.
So for my current use case, I’m using Camunda to process my requests. Based on your suggestion, I should store my request business data independently, and then I can use Camunda to update the status of my request in the independent storage. I would query my storage for my “approved” requests and not Camunda. Is this method more preferable?
So this is actually a big topic, generally speaking you want keep the source of truth for your entity data out of the engine. The engine should be use exclusively to store state related data and the history of state.
There’s is data that might be used to route the process, to set a status, to assign a user to a task, all of that data is relevant to the process in terms of how the state is setup, that kind of data should be stored in the engine for amoung other things the ability to report on your process but also for auditing and debug reasons.