# Setting appVariable from "Run Code" action

**URL:** <https://community.appfarm.io/t/setting-appvariable-from-run-code-action/465>\
**Category:** Ask the community\
**Created:** [May 9, 2023, 9:03am UTC](https://community.appfarm.io/t/setting-appvariable-from-run-code-action/465 "2023-05-09T09:03:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgreodor](https://avatars.discourse-cdn.com/v4/letter/k/5e9695/32.png) [@kgreodor](https://community.appfarm.io/u/kgreodor)\
**Post date:** [May 9, 2023, 9:03am UTC](https://community.appfarm.io/t/setting-appvariable-from-run-code-action/465/1 "2023-05-09T09:03:32Z")

</div>

So. I’m struggling a bit with the “Run Code” action.

I have some logic checking a few things, fetching a data model and then trying to set an AppVariable that is of a “data type” defined in the “global data model”. Everything works until I try to set the model. So I’m wondering what the proper way is to update appVariables from code.

E.g. something like

```auto
const a = model.find(x => x.user == userId)
if (!!a) {
  appVariablies.a = a // does not work
}

```

---

<div class="post-metadata">

**Author:** ![kgreodor](https://avatars.discourse-cdn.com/v4/letter/k/5e9695/32.png) [@kgreodor](https://community.appfarm.io/u/kgreodor)\
**Post date:** [May 9, 2023, 9:15am UTC](https://community.appfarm.io/t/setting-appvariable-from-run-code-action/465/2 "2023-05-09T09:15:20Z")

</div>

Also tested with

```auto
appVariables.a = a._id

```

---

<div class="post-metadata">

**Author:** ![Eirik](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/eirik/32/76_2.png) [@Eirik](https://community.appfarm.io/u/Eirik)\
**Post date:** [May 9, 2023, 10:50am UTC](https://community.appfarm.io/t/setting-appvariable-from-run-code-action/465/3 "2023-05-09T10:50:53Z")

</div>

Edit:

The most appropriate way to update an App Variable would be using the **Update Objects** action node for App Variables and then the function editor for the variable **a**. Here you write you logical checks and then return the desired value for **a**.

Could that be a solution?

If you absolutely need to use the Run Code action node then you may temporary save the value in `window`

```auto
let a = 1
let b = 2

window.runCodeOutput = a * b

resolve()

```

And then using the Update Objects action node to update App Variables with temporary saved window value.

```auto
return window.runCodeOutput

```
