Skip to main content

Update Variable

Update the workflow variables at any time while the workflow is in the RUNNING state. This is similar to the SET_VARIABLE task, except it can be updated anytime while the workflow is running.

Useful when the workflow state is to be updated based on an external trigger, such as terminating a long-running do-while loop with a terminating condition based on the workflow variables.

Input Payload

AttributeDescription
workflowIdThe unique identifier of the workflow whose variable is to be updated.

Client SDK Methods

public abstract Workflow updateVariables(String workflowId, Map<String, Object> variables);

Sample Code

note

You need to register the workflow “update_variable_test” initially.

public void testUpdateVariables() {
ConductorWorkflow<Object> workflow = new ConductorWorkflow<>(workflowExecutor);
workflow.add(new SimpleTask("simple_task","simple_task_ref"));
workflow.setTimeoutPolicy(WorkflowDef.TimeoutPolicy.TIME_OUT_WF);
workflow.setTimeoutSeconds(60);
workflow.setName("update_variable_test");
workflow.setVersion(1);
workflow.registerWorkflow(true, true);

StartWorkflowRequest request = new StartWorkflowRequest();
request.setName(workflow.getName());
request.setVersion(workflow.getVersion());
request.setInput(Map.of());
String workflowId = workflowClient.startWorkflow(request);
assertNotNull(workflowId);

Workflow execution = workflowClient.getWorkflow(workflowId, false);
assertNotNull(execution);
assertTrue(execution.getVariables().isEmpty());

Map<String, Object> variables = Map.of("k1", "v1", "k2", 42, "k3", Arrays.asList(3, 4, 5));
execution = workflowClient.updateVariables(workflowId, variables);
assertNotNull(execution);
assertFalse(execution.getVariables().isEmpty());
assertEquals(variables.get("k1"), execution.getVariables().get("k1"));
assertEquals(variables.get("k2").toString(), execution.getVariables().get("k2").toString());
assertEquals(variables.get("k3").toString(), execution.getVariables().get("k3").toString());

Map<String, Object> map = new HashMap<>();
map.put("k1", null);
map.put("v1", "xyz");
execution = workflowClient.updateVariables(workflowId, map);
assertNotNull(execution);
assertFalse(execution.getVariables().isEmpty());
assertEquals(null, execution.getVariables().get("k1"));
assertEquals(variables.get("k2").toString(), execution.getVariables().get("k2").toString());
assertEquals(variables.get("k3").toString(), execution.getVariables().get("k3").toString());
assertEquals("xyz", execution.getVariables().get("v1").toString());
}