Great follow-up question! No, you cannot retrieve all variables automatically in the TaskSearchResponse. Here’s how variable retrieval works with Tasklist REST API v1:
Variable Retrieval Options
1. Include Specific Variables in Search Response
You can only get variables that you explicitly specify in the search request using the includeVariables
field:
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("candidateGroups", Arrays.asList("group1", "group2"));
requestBody.put("state", "CREATED");
// Specify which variables to include in the response
List<Map<String, Object>> includeVariables = Arrays.asList(
Map.of("name", "customerId"),
Map.of("name", "priority"),
Map.of("name", "amount")
);
requestBody.put("includeVariables", includeVariables);
The response will then include these variables in the variables
field of each task.
2. Get All Variables for Specific Tasks (Recommended Approach)
For retrieving all variables of specific tasks, use the separate endpoint:
@Service
public class TaskService {
public List<TaskWithAllVariables> getTasksWithAllVariables(
List<String> candidateGroups) {
// Step 1: Search for tasks
List<UserTask> tasks = searchTasks(candidateGroups);
// Step 2: Get all variables for each task
return tasks.stream()
.map(task -> {
List<Variable> variables = getAllVariablesForTask(task.getId());
return new TaskWithAllVariables(task, variables);
})
.collect(Collectors.toList());
}
private List<Variable> getAllVariablesForTask(String taskId) {
// Call POST /v1/tasks/{taskId}/variables/search with empty body
return webClient.post()
.uri("/v1/tasks/{taskId}/variables/search", taskId)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(Map.of()) // Empty body returns all variables
.retrieve()
.bodyToMono(VariablesSearchResponse.class)
.map(response -> response.getItems())
.block();
}
}
3. Complete Example with All Variables
Here’s a complete working example:
@Service
public class EnhancedTaskService {
@Autowired
private WebClient tasklistWebClient;
public List<TaskWithVariables> searchTasksWithAllVariables(
List<String> candidateGroups,
Map<String, String> filterVariables) {
// Step 1: Search tasks with variable filters
Map<String, Object> searchRequest = buildSearchRequest(candidateGroups, filterVariables);
TaskSearchResponse searchResponse = tasklistWebClient.post()
.uri("/v1/tasks/search")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(searchRequest)
.retrieve()
.bodyToMono(TaskSearchResponse.class)
.block();
// Step 2: Get all variables for each found task
return searchResponse.getItems().stream()
.map(task -> {
List<Variable> allVariables = getAllTaskVariables(task.getId());
return new TaskWithVariables(task, allVariables);
})
.collect(Collectors.toList());
}
private List<Variable> getAllTaskVariables(String taskId) {
VariablesSearchResponse response = tasklistWebClient.post()
.uri("/v1/tasks/{taskId}/variables/search", taskId)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(Map.of()) // Empty request = all variables
.retrieve()
.bodyToMono(VariablesSearchResponse.class)
.block();
return response.getItems();
}
private Map<String, Object> buildSearchRequest(List<String> candidateGroups,
Map<String, String> filterVariables) {
Map<String, Object> request = new HashMap<>();
request.put("candidateGroups", candidateGroups);
request.put("state", "CREATED");
request.put("pageSize", 50);
// Add variable filters (for filtering, not retrieval)
if (filterVariables != null && !filterVariables.isEmpty()) {
List<Map<String, String>> taskVariables = filterVariables.entrySet().stream()
.map(entry -> Map.of(
"name", entry.getKey(),
"value", "\"" + entry.getValue() + "\"",
"operator", "eq"
))
.collect(Collectors.toList());
request.put("taskVariables", taskVariables);
}
return request;
}
}
// Helper classes
public class TaskWithVariables {
private UserTask task;
private List<Variable> variables;
// constructors, getters, setters
}
Important Considerations
Performance Impact
- Multiple API calls: Getting all variables requires one additional API call per task
- Consider pagination: If you have many tasks, implement proper pagination
- Batch processing: Consider processing tasks in batches to avoid overwhelming the API
Variable Size Limitations
- Large variables: Variables with large content may be truncated in responses
- Use
includeVariables
: You can specify alwaysReturnFullValue: true
for specific variables if needed
Alternative Approach for Better Performance
If you only need specific variables frequently, consider including them directly in the search:
// Include commonly needed variables in the search response
List<Map<String, Object>> includeVariables = Arrays.asList(
Map.of("name", "customerId", "alwaysReturnFullValue", true),
Map.of("name", "priority", "alwaysReturnFullValue", true),
Map.of("name", "processData", "alwaysReturnFullValue", false) // Allow truncation
);
requestBody.put("includeVariables", includeVariables);
References
So the pattern is: search for tasks first, then fetch all variables per task using the dedicated variables endpoint!