How query the completed workflow using workflow id?

Is it possible to query the completed workflow using workflow id?

1 Like

Hello @Vijay_T and welcome.

Do you have advanced visibility enabled? if you do you can use ListWorkflowExecutionsRequest, setting the query setQuery(WorkflowId="yourworkflowid" and ExecutionStatus="Completed")

ListWorkflow API supports pagination, you may want to set pageSize and do recursive calls using the page token in the following call.

Keep in mind that once your retention period is reached your closed workflows (completed and failed) will be deleted, see this related post. Running more than one instance with same workflowId in parallel - #3 by swaroop

If you don’t have advance visibility enabled you can still use ListWorkflowExecutionsRequest but the types of queries you can do are limited, i.e you can not use operators like AND Visibility | Temporal Documentation

1 Like

You can query by creating a workflow stub by WorkflowID.

I have tried to query a completed workflow and I’m getting WorkflowNotFoundException. I have tried a similar code as provided in the temporal-java-samples repository:

// The workflowOptions has the ID of a workflow in the Completed state.
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
// Querying the workflow in the Completed state throws an exception.

Check if that completed execution was removed due to your namespace retention setting.

tctl wf desc -w <wfid> -r <runid>

(runid param can be optional)

Hi @tihomir, it’s not removed, I can see it from the Temporal UI:

By the way, it’s possible to use the ListWorkflowExecutionsRequest API to query the completed workflow by ID, as @antonio.perez has mentioned above. In the temporal-java-samples project, there is an example in the file io/temporal/samples/listworkflows/

  private static ListWorkflowExecutionsResponse getExecutionsResponse(String query) {
    ListWorkflowExecutionsRequest listWorkflowExecutionRequest =
    ListWorkflowExecutionsResponse listWorkflowExecutionsResponse =
    return listWorkflowExecutionsResponse;

This API is very useful, the only limitation is the need for Elasticsearch if using multiple query filters.

Running the code above with getExecutionsResponse("WorkflowId = 'LH0ixwnILCpkWE3CsZVMWqSo'"), I get the correct workflow execution:

executions {
  execution {
    workflow_id: "LH0ixwnILCpkWE3CsZVMWqSo"
    run_id: "9616c2ed-0e82-41e7-aae8-df13cfbeb485"
  type {
    name: "SignupFlow"
  start_time {
    seconds: 1675448407
    nanos: 657057000
  close_time {
    seconds: 1675448468
    nanos: 491348000
  history_length: 28
  execution_time {
    seconds: 1675448407
    nanos: 657057000
  memo {
  task_queue: "signups"

Listing your executions in web ui and via ListWorkflowExecutions pulls data from your visibility store.
Tctl command

tctl wf desc -w -r

(you could do same via DescribeWorkflowExecution api) pull from your primary persistence.
Would need to look if the data is still available in both places for this execution.
If its only available in persistence you may be running into high visibility latencies? Also check history service logs. Server metrics for visibility latencies if helps:
histogram_quantile(0.95, sum(rate(task_latency_bucket{operation=~"VisibilityTask.*", service_name="history"}[1m])) by (operation, le))

By the way, ListWorkflowExecutions is paginated so would need to check if your response has a nexPageToken and then use that token to pull next page of results and so on to make sure you get all the results.

1 Like

Also just to note the latest java sdk 1.181. release does include a new convenience api:
WorkflowClient#listExecutions that you could use as well

1 Like

Very nice, thanks.