How to distinguish between local variables and global variables in a historic query

When starting a process instance, I set a variable which name is “amount” and value is 500L. And then, in a user task of the process instance, I set a local variable with the same name of “amount” but its value is 2000L using runtimeService. setVariableLocal() method.

After that, I tried to use historyService.createHistoricVariableInstanceQuery().variableName("amount").list() method to query the variable, and the there’re 2 HistoricVariableInstances in the result. Thus, how can I recognize which one is local variable?

Can you toString the two objects and post them to compare

HistoricVariableInstanceEntity[id=17, processDefinitionKey=localVariableTest, processDefinitionId=localVariableTest:1:3, rootProcessInstanceId=4, removalTime=null, processInstanceId=4, taskId=null, executionId=10, tenantId=null, activityInstanceId=Task_2:16, caseDefinitionKey=null, caseDefinitionId=null, caseInstanceId=null, caseExecutionId=null, name=amount, createTime=Fri Apr 26 08:03:50 CST 2019, revision=0, serializerName=long, longValue=1500, doubleValue=null, textValue=1500, textValue2=null, state=CREATED, byteArrayId=null] and HistoricVariableInstanceEntity[id=5, processDefinitionKey=localVariableTest, processDefinitionId=localVariableTest:1:3, rootProcessInstanceId=4, removalTime=null, processInstanceId=4, taskId=null, executionId=4, tenantId=null, activityInstanceId=4, caseDefinitionKey=null, caseDefinitionId=null, caseInstanceId=null, caseExecutionId=null, name=amount, createTime=Fri Apr 26 08:03:49 CST 2019, revision=0, serializerName=long, longValue=500, doubleValue=null, textValue=500, textValue2=null, state=CREATED, byteArrayId=null]

Any further information please?

Take a look at

Thanks a lot for your ref. It is said that

If the variable has been set locally on the user task, only then is the task id not null, since the scope of the variable is the user task.

but in my case, I’m afraid the two taskIds are all null as shown in the console. Only activityInstanceId and executionId are different but they are all runtime value and it is hard to know the value before it is created.