This example demonstrates how to work with json variables in embedded forms.
This example uses Jakarta API. It is compatible with the latest releases of Camunda Tomcat and WildFly distributions.
The process instance is started using a form. The form is a plain HTML form which is displayed inside Camunda Tasklist (or inside a custom application using the camunda-bpm-sdk-js library).
<form role="form" class="form-horizontal">
<script cam-script type="text/form-script">
var customer = $scope.customer = {};
camForm.on('form-loaded', function() {
// declare a 'json' variable 'customer'
camForm.variableManager.createVariable({
name: 'customer',
type: 'json',
value: customer
});
});
</script>
<div class="control-group">
<label class="control-label" for="firstName">First Name</label>
<div class="controls">
<input id="firstName" class="form-control" type="text" ng-model="customer.firstName" required />
</div>
</div>
<!-- ... Additional fields ommitted -->
</div>
</form>
The custom java script creates a Javascript Object and binds it to the angular $scope
of the form
as a variable named customer
. We then hook into the lifecycle of camunda SDK JS Form and
create a process variable named customer
and provide as type information 'json' used for serialization.
The form itself is a plain angular js form (see ng-model
binding of input field).
In a task form, an existing Json variable can be accessed using custom Javascript. The pattern is to first fetch the value of the variable and then bind it to an angular scope variable:
camForm.on('form-loaded', function() {
// tell the form SDK to fetch the json variable named 'customer'
camForm.variableManager.fetchVariable('customer');
});
camForm.on('variables-fetched', function() {
// work with the variable (bind it to the current AngularJS $scope)
$scope.customer = camForm.variableManager.variableValue('customer');
});
In a JavaDelegate, you can access the Json variable with the help of the Spin libraries. You can get a typed variable as JsonValue
and use the Spin API to set an additional attribute:
public void execute(DelegateExecution execution) throws Exception {
JsonValue jsonCustomer = execution.getVariableTyped("customer");
jsonCustomer.getValue().prop("isValid", true);
execution.setVariable("customer", jsonCustomer);
}
- Download a Camunda Platform Distribution
- Checkout this repository using Git
- Build the example using
mvn clean package
- Deploy the
.war
file located in thetarget/
folder to the server - Open Camunda Tasklist using the URL http://localhost:8080/camunda/app/tasklist
- Start a new instance of the Process.