How to fetch Json (Spin) data from history variables update?

Hi,

I need to get values from a history Json variable. The code I used now is:

processInstanceId = execution.getProcessInstanceId()
historyService = execution.getProcessEngineServices().getHistoryService()
variable_instance_id = execution.getVariableInstance("Variable_Name").getId()
variable_history_updates = historyService.createHistoricDetailQuery()
				  					 .processInstanceId(processInstanceId)
									 .variableUpdates()
									 .variableInstanceId(variable_instance_id)
									 .list()
									 
Variable_Name_original = variable_history_updates[0];

How can I get the json variable from Variable_Name_original ?

Are you trying to get each historical value of the variable?

No, I need only for the first (after I intend to order by timestamp).

But, I can try. You mean I made a loop and how can I ger an specific property for each item?
Like this ?

for (update : variable_history_updates) {
println(update.prop("XXX").stringValue())
}
DETAILS------------>
[HistoricDetailVariableInstanceUpdateEntity[variableName=myJsonVariable, variableInstanceId=8eef66d3-3f44-11e8-9e0f-0242ac130002, revision=0, serializerName=null, longValue=null, doubleValue=null, textValue=null, textValue2=null, byteArrayId=null, activityInstanceId=Task_19mc7r5:8ee1f951-3f44-11e8-9e0f-0242ac130002, eventType=null, executionId=8edffd7f-3f44-11e8-9e0f-0242ac130002, id=8eef66d5-3f44-11e8-9e0f-0242ac130002, processDefinitionId=8edffd7f-3f44-11e8-9e0f-0242ac130002, processInstanceId=8edffd7f-3f44-11e8-9e0f-0242ac130002, taskId=null, timestamp=Fri Apr 13 14:00:32 EDT 2018], HistoricDetailVariableInstanceUpdateEntity[variableName=myJsonVariable, variableInstanceId=8eef66d3-3f44-11e8-9e0f-0242ac130002, revision=1, serializerName=null, longValue=null, doubleValue=null, textValue=null, textValue2=null, byteArrayId=null, activityInstanceId=Task_10dfa25:8ef1d7d9-3f44-11e8-9e0f-0242ac130002, eventType=null, executionId=8edffd7f-3f44-11e8-9e0f-0242ac130002, id=8efa153c-3f44-11e8-9e0f-0242ac130002, processDefinitionId=8edffd7f-3f44-11e8-9e0f-0242ac130002, processInstanceId=8edffd7f-3f44-11e8-9e0f-0242ac130002, taskId=null, timestamp=Fri Apr 13 14:00:32 EDT 2018]]
JSON Values In order of occurance------------>
{"cat":123,"dog":321}
{"horse":111}
First Value------------>
{"cat":123,"dog":321}

javascript:

var system = java.lang.System

var processInstanceId = execution.getProcessInstanceId()
var historyService = execution.getProcessEngineServices().getHistoryService()
var variable_instance_id = execution.getVariableInstance("myJsonVariable").getId()
var variable_history_updates = historyService.createHistoricDetailQuery()
                  .processInstanceId(processInstanceId)
                  .variableUpdates()
                  .variableInstanceId(variable_instance_id)
                  .orderByVariableRevision() 
                  .asc()
                  .list()


system.out.println("DETAILS------------>");
system.out.println(variable_history_updates.toString());

system.out.println("JSON Values In order of occurance------------>");

for each (var item in variable_history_updates) {
  system.out.println(item.getValue().toString())
}

system.out.println("First Value------------>");
system.out.println(variable_history_updates.get(0).getValue().toString())

Also make sure you that your history query is not occurring in the same transactions as when you set your variable(s). You can add “async after” on the last task before you are doing this query to ensure that the variable updates have been committed to the db

2 Likes

Thank you, it is exactly what I need.

PS: Good to share about “async after” … it saves a lot of hours !