Hi all. I want to crate a script in groovy format in a task listener that attaches to a user task.I used {{some_name}} as a logical placeholder in formKey in bpmn. I want to replace the placeholder at the runtime with the actual value that comes from an api. for example the value is https://xyz.com
I wrote the script like this :
import groovy.json.JsonSlurper
import java.util.regex.Matcher
import java.util.regex.Pattern
def formService = task.execution.getProcessEngineServices().getFormService()
String processDefinitionId = task.execution.getProcessDefinitionId()
String taskDefinitionKey = task.getTaskDefinitionKey()
String formKey = formService.getTaskFormKey(processDefinitionId, taskDefinitionKey)
Pattern placeholderPattern = Pattern.compile("\\{\\{(.*?)}}");
Matcher matcher = placeholderPattern.matcher(formKey);
StringBuilder result = new StringBuilder();
List<Object> results = new ArrayList<>();
if (matcher.find()) {
String matchName = matcher.group(1).trim();
matcher.appendReplacement(result, matchName);
results.add(matchName);
}
def variableName = matcher.appendTail(result);
def client = new URL("https://name.com/d/api/v1.0/systemVariable/variable?vn=%s".formatted(variableName));
def headers = ['Authorization': "Bearer token value"]
HttpURLConnection connection = (HttpURLConnection) client.openConnection()
connection.setRequestMethod('GET')
headers.each { key, value ->
connection.setRequestProperty(key, value)
connection.content
def responseCode = connection.getResponseCode()
if (responseCode == 200) {
def responseStream = connection.getInputStream()
def responseText = responseStream.getText()
def jsonResponse = new JsonSlurper().parseText(responseText)
if (jsonResponse["value"] != null) {
def expressionManager = task.execution.getProcessEngineServices()
.getProcessEngineConfiguration()
.getExpressionManager()
def formKeyExpression = expressionManager.createExpression(jsonResponse["value"].toString())
task.getTaskDefinition().setFormKey(formKeyExpression);
}
}
}
//}
There is a problem with this line of script: task.getTaskDefinition().setFormKey(formKeyExpression)
that changes the form key for entire definition which remove the {{some_name}} for next instance will run. How to do this? I want to change the formKey only for this instance not entire instances.