# Sharing values between workflow and activities

**URL:** https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429
**Category:** Community Support
**Tags:** go-sdk, context-propagators
**Created:** [July 22, 2022, 5:01am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429 "2022-07-22T05:01:47Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Sayuru](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sayuru/32/2537_2.png) [@Sayuru](https://community.temporal.io/u/Sayuru)
#### Post date: [July 22, 2022, 5:01am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/1 "2022-07-22T05:01:47Z")

</div>

Hey there,  
I have a workflow with multiple activities. I want to update the context of the workflow with some values (flags) in the first activity and I want to access these flags in the subsequent activities in the same workflow without passing them as parameters. The values fetched should be local to the workflow run, coz different workflow runs of the same workflow type will have different flag values.

Could you please suggest a way forward for me? Thanks.

---

<div class="post-metadata">

### Author: ![Rob\_Temporal](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/rob_temporal/32/1079_2.png) [@Rob\_Temporal](https://community.temporal.io/u/Rob_Temporal)
#### Post date: [July 22, 2022, 8:46am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/2 "2022-07-22T08:46:23Z")

</div>

If the activity depends on these flags, why do you not want to pass them to the activity?

---

<div class="post-metadata">

### Author: ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)
#### Post date: [July 22, 2022, 2:59pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/3 "2022-07-22T14:59:09Z")

</div>

Just to add, one thing to look into into could be [sessions api](https://docs.temporal.io/concepts/what-is-task-routing/#sessions) with Go SDK to assure that multiple activities are executed on the same worker process / host, see [file processing sample](https://github.com/temporalio/samples-go/tree/main/fileprocessing).

This would allow you for example to have one activity write a result to file system / local store / memory of that worker host and know it will be accessible to the rest of activities that run on the same host, just idea.

---

<div class="post-metadata">

### Author: ![Sayuru](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sayuru/32/2537_2.png) [@Sayuru](https://community.temporal.io/u/Sayuru)
#### Post date: [July 23, 2022, 12:32am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/4 "2022-07-23T00:32:58Z")

</div>

Thanks dor the replies.  
The reason why I don’t want to pass them to the activities is that it’s messy. There’s a lot of business logic code and I want to try and separate this.  
I was trying to add the flags to the workflow context and access them in the activity context. No success.  
I’ll try the file sharing method that you have suggested.

---

<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: [July 23, 2022, 12:48am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/5 "2022-07-23T00:48:38Z")

</div>

You can implement your own ContextPropagator to propagate any needed values from workflow to activity context.

---

<div class="post-metadata">

### Author: ![Sayuru](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sayuru/32/2537_2.png) [@Sayuru](https://community.temporal.io/u/Sayuru)
#### Post date: [July 23, 2022, 1:28am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/6 "2022-07-23T01:28:39Z")

</div>

Thanks for the response mate. My question is would updating the context in one workflow affect the values in other contexts? In the examples I referred for context propagators, values are passed from the worker to workflow to activity via context propagators. What I want is an activity to fetch the flags and other activities to use the flags within the same 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: [July 23, 2022, 4:54pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/7 "2022-07-23T16:54:43Z")

</div>

You can implement a context propagator to propagate value of “flags” key. Then the first activity would load the flags and then call `context.WithValue(ctx, "flags", flags)`. After that, all activities are going to have this value propagated to their contexts.

---

<div class="post-metadata">

### Author: ![Sayuru](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sayuru/32/2537_2.png) [@Sayuru](https://community.temporal.io/u/Sayuru)
#### Post date: [July 23, 2022, 5:50pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/8 "2022-07-23T17:50:11Z")

</div>

Thank you very much for the explanation. I’ll do this. 😌

---

<div class="post-metadata">

### Author: ![atullimaye19](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@atullimaye19](https://community.temporal.io/u/atullimaye19)
#### Post date: [September 13, 2022, 7:37pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/9 "2022-09-13T19:37:19Z")

</div>

Is it available for java sdk ?

---

<div class="post-metadata">

### Author: ![antonio.perez](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/antonio.perez/32/2417_2.png) [@antonio.perez](https://community.temporal.io/u/antonio.perez)
#### Post date: [September 13, 2022, 7:59pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/10 "2022-09-13T19:59:27Z")

</div>

Hi @atullimaye19

it is available through `org.slf4j.MDC` , see this test code [sdk-java/ContextPropagationTest.java at master · temporalio/sdk-java · GitHub](https://github.com/temporalio/sdk-java/blob/master/temporal-sdk/src/test/java/io/temporal/workflow/ContextPropagationTest.java)

---

<div class="post-metadata">

### Author: ![A\_V](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/a_v/32/3302_2.png) [@A\_V](https://community.temporal.io/u/A_V)
#### Post date: [February 9, 2023, 12:51pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/11 "2023-02-09T12:51:15Z")

</div>

@maxim /@antonio.perez can the same context thing could be achieved in typescript SDK, if yes how can we do it ?

---

<div class="post-metadata">

### Author: ![antonio.perez](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/antonio.perez/32/2417_2.png) [@antonio.perez](https://community.temporal.io/u/antonio.perez)
#### Post date: [February 9, 2023, 3:52pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/12 "2023-02-09T15:52:11Z")

</div>

Hello @A_V ,

please see [Logging and Sinks in TypeScript SDK | Legacy documentation for Temporal SDKs](https://legacy-documentation-sdks.temporal.io/typescript/logging?_ga=2.55106155.1935929247.1675674503-764904631.1658231508#opentelemetry-tracing)

Let me know if it helps.

---

<div class="post-metadata">

### Author: ![wanghaotime](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/wanghaotime/32/4516_2.png) [@wanghaotime](https://community.temporal.io/u/wanghaotime)
#### Post date: [November 6, 2023, 9:21am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/13 "2023-11-06T09:21:57Z")

</div>

Hi bro, I have a workflow with multiple activities, all activities need check a common data struct, How can I store the common data struct, all activities can get the common data struct

---

<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: [November 6, 2023, 5:59pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/14 "2023-11-06T17:59:13Z")

</div>

Make these activities methods of a struct. See the greetings sample.

---

<div class="post-metadata">

### Author: ![wanghaotime](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/wanghaotime/32/4516_2.png) [@wanghaotime](https://community.temporal.io/u/wanghaotime)
#### Post date: [November 7, 2023, 4:01am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/15 "2023-11-07T04:01:15Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/c/cb0443e692079543786b2b6fd4a5021176382bd0.png)  
this must be create new worker ,but I want to exec activity in workflow definition at the time, every time to exec workflow, the common data is not same

---

<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: [November 7, 2023, 5:36pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/16 "2023-11-07T17:36:00Z")

</div>

Temporal activities are like serivces. They have an API. Why do you need a global common structure shared by all of them?

---

<div class="post-metadata">

### Author: ![wanghaotime](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/wanghaotime/32/4516_2.png) [@wanghaotime](https://community.temporal.io/u/wanghaotime)
#### Post date: [November 8, 2023, 2:52am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/17 "2023-11-08T02:52:16Z")

</div>

This depend on our business scenario, In our business process that all activities need to use the global common structure to calculate result, but every time we execute a workflow use a different value of global common structure. so I need a method to store the different value of global common structure to share by all of activities at every execution of the workflow .Can you give me some methods? thanks

---

<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: [November 8, 2023, 4:52pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/18 "2023-11-08T16:52:53Z")

</div>

You pass this structure to every activity invocation and keep it inside the workflow for persistence.

Another approach is to cache this structure in memory of a worker process and route all the workflow activities to that specific process. If the process dies, then the whole sequence of activities needs to be executed in a different process.

---

<div class="post-metadata">

### Author: ![wanghaotime](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/wanghaotime/32/4516_2.png) [@wanghaotime](https://community.temporal.io/u/wanghaotime)
#### Post date: [November 9, 2023, 11:37am UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/19 "2023-11-09T11:37:25Z")

</div>

i think this design is not appropriate, you can build a connection between workflow.Context and context.Context, that is to say ,the data that set in workflow.Context can be acquire by context.Context in activity.  
the another way is to set common data before the execution of activity, For example below

```auto
a := &Activities{Data:data}
workflow.ExecuteActivity(ctx, a.Activity1)

```

---

<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: [November 9, 2023, 6:07pm UTC](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429/20 "2023-11-09T18:07:19Z")

</div>

You are still proposing passing this data to activity. But instead of having an explicit interface you pass it implicitly. I don’t understand why you are against cleanly defined interfaces and what to pass hidden arguments.

> a := &Activities{Data:data}  
> workflow.ExecuteActivity(ctx, a.Activity1)

This is not supported.

[Next page](https://community.temporal.io/t/sharing-values-between-workflow-and-activities/5429.md?page=2)
