I am using the REST_API. I know this can probably be done with JAVA but I am using it as a service. So it must be done with the REST_API
When I complete a task, variables are saved to history. However, it does not persist the task id. Is there any way to make that happen?

A group of variables are saved with each task, and there is no way to know which set belongs to which task! I guess I can add some metadata but would rather not.
If that is not possible-
Would there be any way I can get back the internal variable UUID so I can update it myself?
I also had a similar problem with comments.
I’m almost ready to prefix the variablename with the task UUID.
engine-rest/task/task_id/variables/var_name
will not store the task id in the history table.
@lenny_h, there’s a common field EXECUTION_ID_ stored in both tables ACT_HI_TASKINST and ACT_HI_VARIABLE .
So you can query in ACT_HI_VARIABLE table with EXECUTION_ID_ by using EXECUTION_ID_ from ACT_HI_TASKINST table like below:
select * from act_hi_varinst where EXECUTION_ID_ IN (select EXECUTION_ID_ from
act_hi_taskinst where EXECUTION_ID_='d029c7dd-7d61-11ea-b596-507b9dc4ed46');
For rest api, you have to query for both tables, and aggregate the results by EXECUTION_ID_ .
EXECUTION_ID is pretty much the PROC_INST_ID_
Still can’t tell which variable belongs to a particular atomic task.
My solution is to prefix the variable with the TASK_ID_. Then I have a simple query. I could always add an index if I go over 100K records or so, or query by instance first.

I’m curious Why won’t the API store TASK_ID_? I understand the variables could be in global scope but in the real world it seems you want to hold state about certain events that happen as the workflow progresses.
Read more about variable-scopes-and-variable-visibility and handling-data-in-processes.
Read this post: https://groups.google.com/d/msg/camunda-bpm-users/KvrClQpB05k/LOXndYRT6EsJ
If you set the variables in global scope i.e., process variable (via Rest or Java api) it will not set the value for TASK_ID_ column. So TASK_ID_ column value will be always null in the ACT_HI_VARINST table.
setting process variables (global scope) via Rest Api:
setting process variables (global scope) via Java Api:
runtimeService.setVariable(executionId, variableMap);
When the variables are set in local scopes i.e., task variables (via Rest or Java api), then the TASK_ID_ column will be set with taskId.
setting task variables (local scope) via Rest Api:
setting task variables (local scope) via Java Api:
taskService.setVariableLocal(taskId, variableMap);
Then if you query for ACT_HI_TASKINST with below query:
select * from act_hi_varinst where TASK_ID_ is not null;
Results will be like: