# Accessing WorkFlow attributes after execution, using history

**URL:** <https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [February 9, 2021, 4:51pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470 "2021-02-09T16:51:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramprasad\_Indarapu](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ramprasad_indarapu/32/718_2.png) [@Ramprasad\_Indarapu](https://community.temporal.io/u/Ramprasad_Indarapu)\
**Post date:** [February 9, 2021, 4:51pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/1 "2021-02-09T16:51:31Z")

</div>

I have a usecase, where i want to extract the a json tree(state) built in workflow execute method. Following is the sample code.

```java
public class TicketWorkFlowImpl implements TicketWorkFlow {

  @Override
  public String execute(Step[] steps){
    JsonNode resultNode..
    //process steps and build resultNode

  }
}

```

After the workflow is executed, there is another api where i want to fetch the result. I want to use replay or history to fetch the resultNode.  
One solution i am thinking is to use the history of events and fetch from the events data

```java
 GetWorkflowExecutionHistoryRequest request = getWorkflowExecutionHistoryRequest(workFlowId);
    GetWorkflowExecutionHistoryResponse resultHistory =
        service.blockingStub().getWorkflowExecutionHistory(request);

    Stream<HistoryEvent> events = resultHistory.getHistory().getEventsList().stream();

```

But as this is not used as activity input or output parametr i can not get the same.

The second solution i see is create a dummy Activity and pass the resultNode so that it is stored as part of events data.

```java
public class TicketWorkFlowImpl implements TicketWorkFlow {

  @Override
  public String execute(Step[] steps){
    JsonNode resultNode..
    //process steps and build resultNode
     CallResultActivity.record(resultNode)//This is a temporal activity
  }
}

```

Are there any better ways for the usecase mentioned? Are there any drawbacks with the second solution

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [February 9, 2021, 6:36pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/2 "2021-02-09T18:36:36Z")

</div>

There are two ways to solve your problem.

## 1. Use workflow result

Return the `resultNode` as the workflow result. Then use the `WorkflowStub.getResultAPI` to extract the result.

```java
WorkflowClient client = ...;
WorkflowStub workflow = client.newUntypedWorkflowStub("workflowId", Optional.empty(), Optional.of("TicketWorkFlow"));
JsonNode result = workflow.getResult(1, TimeUnit.SECONDS, JsonNode.class);

```

## 2. Use query

Add a query method to the workflow interface and call it to return the `resultNode`. Note that queries work even for closed workflows (up to the retention period).

```java
@WorkflowInterface
public interface TicketWorkflow {
    @WorkflowMethod
    String execute(Step[] steps);

    @QueryMethod
    JsonNode getResultNode();
}

public class TicketWorkFlowImpl implements TicketWorkFlow {

    private JsonNode resultNode;

    @Override
    public JsonNode execute(Step[] steps){
        //process steps and build resultNode

    }

    JsonNode getResultNode() {
        return resultNode;
    }
}

```

Then use the query to retrieve result:

```java
    WorkflowClient client = WorkflowClient.newInstance();
    TicketWorkFlow workflow = client.newWorkflowStub(TicketWorkFlow, "workflowId", Optional.empty());
    JsonNode result = workflow.getResultNode();

```

---

<div class="post-metadata">

**Author:** ![Ramprasad\_Indarapu](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ramprasad_indarapu/32/718_2.png) [@Ramprasad\_Indarapu](https://community.temporal.io/u/Ramprasad_Indarapu)\
**Post date:** [February 10, 2021, 3:33am UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/3 "2021-02-10T03:33:50Z")

</div>

> [@maxim](#):
>
> 1. Use query

Thank you @maxim, Using the second solution. It is working

---

<div class="post-metadata">

**Author:** ![Ramprasad\_Indarapu](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ramprasad_indarapu/32/718_2.png) [@Ramprasad\_Indarapu](https://community.temporal.io/u/Ramprasad_Indarapu)\
**Post date:** [March 2, 2021, 5:44pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/4 "2021-03-02T17:44:46Z")

</div>

@maxim

We implemented the 2nd solution mentioned.

```auto
    JsonNode getResultNode() {
        return resultNode;
    }

```

But we have a case where we have parentflow - invoking childflow

```auto
    WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
    WorkflowClient client = WorkflowClient.newInstance(service);
    TestWorkFlow jw = client.newWorkflowStub(TestWorkFlow.class, workFlowId, Optional.empty());
    JsonNode node = jw.getNodeResult(path);

```

When I use this way, the main flow is replayed but it is trying to create new childflow and I see this issue

```auto
Caused by: java.lang.IllegalStateException: COMMAND_TYPE_START_CHILD_WORKFLOW_EXECUTION doesn't match EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_INITIATED with EventId=5
	at io.temporal.internal.statemachines.WorkflowStateMachines.assertMatch(WorkflowStateMachines.java:784)
	at io.temporal.internal.statemachines.WorkflowStateMachines.validateCommand(WorkflowStateMachines.java:750)
	at io.temporal.internal.statemachines.WorkflowStateMachines.handleCommandEvent(WorkflowStateMachines.java:272)
	at io.temporal.internal.statemachines.WorkflowStateMachines.handleEventImpl(WorkflowStateMachines.java:199)
	at io.temporal.internal.statemachines.WorkflowStateMachines.handleEvent(WorkflowStateMachines.java:178)

Please let me know if I am missing anything
```

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [March 2, 2021, 5:56pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/5 "2021-03-02T17:56:03Z")

</div>

It looks like your workflow code is not determninistic. Make sure that it follows [all the constraints](https://docs.temporal.io/docs/java-implementing-workflows#workflow-implementation-constraints) outlined in the documentation.

How do you start the child workflow?

---

<div class="post-metadata">

**Author:** ![Ramprasad\_Indarapu](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ramprasad_indarapu/32/718_2.png) [@Ramprasad\_Indarapu](https://community.temporal.io/u/Ramprasad_Indarapu)\
**Post date:** [March 2, 2021, 6:02pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/7 "2021-03-02T18:02:09Z")

</div>

Thanks, the below is child workflow creation code  
WorkflowClientOptions clientOptions =  
WorkflowClientOptions.newBuilder().setNamespace(DEFAULT\_NAME\_SPACE).build();  
WorkflowClient client = WorkflowClient.newInstance(service, clientOptions);  
ChildWorkflowOptions options = ChildWorkflowOptions.newBuilder()  
.setTaskQueue(Shared.HELLO\_WORLD\_CHILD\_TASK\_QUEUE)  
.setWorkflowId(workFlowId)  
.build();  
TestWorkflow workflow = Workflow.newChildWorkflowStub(TestWorkflow.class, options);  
This is the code i am using for child workflow

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [March 2, 2021, 6:44pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/8 "2021-03-02T18:44:41Z")

</div>

I’m confused. Why are you using WorkflowClient inside the workflow code? It is not allowed as any other RPC calls.

---

<div class="post-metadata">

**Author:** ![Ramprasad\_Indarapu](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ramprasad_indarapu/32/718_2.png) [@Ramprasad\_Indarapu](https://community.temporal.io/u/Ramprasad_Indarapu)\
**Post date:** [March 3, 2021, 1:54am UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/9 "2021-03-03T01:54:47Z")

</div>

Oh!. Thanks @maixim. I removed that and it is working now.  
TestWorkflow workflow = Workflow.newChildWorkflowStub(TestWorkflow.class);

Thanks once again

---

<div class="post-metadata">

**Author:** ![Ramprasad\_Indarapu](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ramprasad_indarapu/32/718_2.png) [@Ramprasad\_Indarapu](https://community.temporal.io/u/Ramprasad_Indarapu)\
**Post date:** [April 7, 2021, 11:37am UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/10 "2021-04-07T11:37:46Z")

</div>

@maxim

We implemented the 2nd solution mentioned ealier

```auto
    JsonNode getResultNode() {
        return resultNode;
    }

```

When we call getResultNode only the mainflow is replayed at activity level and subflow activities are not replayed. As we are building some variables in the subflows(using the redisjson), the same is not getting calculated again.  
I think the child workflows are replayed at workflow level, how can we make it to replay activity level.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [April 7, 2021, 4:09pm UTC](https://community.temporal.io/t/accessing-workflow-attributes-after-execution-using-history/1470/11 "2021-04-07T16:09:38Z")

</div>

I recommend do not think about replay when writing workflows. It doesn’t bring any value but just confuses you.

Would you explain what you are trying to achieve that is not supported by the query?
