Access HistoryService from Javascript?

Hi there,

I need to query some data from “old” (=finished) process instances.

This data should be displayed directly after the user clicks some buttons on a task form (I’m using embedded forms and the Tasklist application).

In an ExecutionListener this would be an easy one using the HistoryService class.

So I wonder if it is possible to access the HistoryService (and other Process Engine functions) directly from JavaScript from within an embedded task form.

Or do you have any other suggestions how to solve this?

Cheers,
Frank

Hi Frank_Bitzer,

You can query the history using REST API see below link

(REST API) History

You can add the following java script code to your form

<script cam-script type="text/form-script">
	inject([ '$scope', 'camAPI', function($scope, camAPI) {
		$scope.displayFinishedProcessInstances = function () {
				var History = camAPI.resource('history');
				
				History.processInstance({finished: true}, function(err, data) {
					if (!err) {
						alert('Data: ' + JSON.stringify(data)); 
						$scope.processInstances = data;
					}
				});
			};
	}]);
</script>

Notice: Based on above code, your custom button should invoke displayFinishedProcessInstances function
and the result will be saved on processInstances variable

For more information about history service please refer to below link
history service

1 Like

Wow, this works. Thank you very much.

However I’m struggling loading actual variable values for the finished process instances returned by the history service.
There seems to be a variable service as well, but I think it only is capable of querying variables for non-historic process instances.

Here is what I tried:

inject([ '$scope', 'camAPI', function($scope, camAPI) {
	$scope.displayFinishedProcessInstances = function () {
			var History = camAPI.resource('history');
			
			History.processInstance({finished: true, processDefinitionKey:'onboarding'}, function(err, data) {
				if (!err) {
					//alert('Data: ' + JSON.stringify(data)); 
					$scope.processInstances = data;

					angular.forEach($scope.processInstances, function(pi) {
						 alert("Found a finished process instance with id: " + pi.id);


						var Variable = camAPI.resource('variable');
			
						Variable.instances({processInstanceIdIn: pi.id}, function(err, data) {
							if (!err) {
								alert('Variables: ' + JSON.stringify(data)); 
								
							}
						});

					});

				}
			});
		};
}]);

But I get a 400 Bad Request for the call to variable service.

Any ideas?

Hi Frank_Bitzer,
Use camAPI.resource(‘variable-instance’)

Thank you once again. However, this does not work.

var Variable = camAPI.resource('variable-instance');

results in “Variable” being undefined.

Do I have to add any special dependencies?

Hi Frank_Bitzer,
Value passed for processInstanceIdIn parameter should be an array of String
{processInstanceIdIn: [pi.id]}

And use camAPI.resource(‘variable’) not camAPI.resource(‘variable-instance’) “I was wrong”
As I reviewed https://github.com/camunda/camunda-bpm-sdk-js/blob/master/lib/api-client/index.js
and noticed that ‘variable’ keyword is used to reference the variable resource

Yes, that works, but according to my tests it only can query for variable information in active/running process instances.

I need to query historic variable data from finished process instances.

So it looks like I have to implement a client for this purpose by myself.

Anyway, thank you very much!

Ok guys here is how I solved it.
Probably not the most elegant solution ever seen, but I’m not familar with AngularJS at all…

inject([ '$scope', 'camAPI', '$http', function($scope, camAPI, $http) {
	$scope.displayFinishedProcessInstances = function () {
			var History = camAPI.resource('history');
			
			History.processInstance({finished: true, processDefinitionKey:'onboarding'}, function(err, data) {
				if (!err) {

					angular.forEach(data, function(pi) {
						 alert("Found a finished process instance with id: " + pi.id);

						var lastName;
						
                                                    // ACCESS THE HISTORIC VARIABLE INFORMATION
						$http.get('/camunda/api/engine/engine/default/history/variable-instance',
							{ params: {
								processInstanceId : pi.id,
								variableName : 'my_var_name'
								}
							}).
					                success(function(data) {
								
        						           lastName = data[0].value;

					        
				}
			});
		};
}]);
1 Like