I am designing process definitions in flowable-modeler and do baisc testing (with script tasks and/or mock java service tasks/classes) of these in flowable-task.
In the actual application the process instance ist created by “startProcessInstanceById” with a “businessKey” set.
I would also like to set a business key, when starting a process instance out of flowable-task.
Would this be possible from a “start-form”, “start-event” execution listener or somehow different?
The start-form is rendered/shown, before the process instance is created.
I usually include an “Initialise” ScriptTask right at the beginning of my process to initialise, validate and sanitise variables.
One “work-around” is to set the Business Key there using
execution.setBusinessKey("MyBusinessKey");
You can also take it one step further
var businessKey = someInputId;
var businessKeyExists = runtimeService
.createProcessInstanceQuery()
.processInstanceBusinessKey(businessKey, execution.processDefinitionKey)
.count() > 0;
if (businessKeyExists) {
throw new java.lang.RuntimeException("Process Instance with Business Key already exists");
}
var historicBusinessKeyExists = historyService
.createHistoricProcessInstanceQuery()
.processInstanceBusinessKey(businessKey)
.processDefinitionKey(execution.processDefinitionKey)
.count() > 0;
if (historicBusinessKeyExists) {
throw new java.lang.RuntimeException("Process Instance with Business Key already exists");
}
runtimeService.updateBusinessKey(execution.processInstanceId, businessKey);
If you keep the “Initialise” ScriptTask synchronous, this should throw the Exception back to the user. Now, unfortunately there is a bug in the flowable-ui-task-app/src/main/resources/static/workflow/scripts/services/form-service.js that server errors are not displayed to the user, but once that is fixed the user should have some idea that the business key is not unique.
UPDATE: The first version did not take into account historic processes with the same business key. I also neglected the fact that simply setting the business key on the execution variable does update the historic process instance.