# Trigger Workflow On Variable Change

**URL:** https://community.weweb.io/t/trigger-workflow-on-variable-change/4717
**Category:** Ask us anything
**Created:** [October 3, 2023, 8:11pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717 "2023-10-03T20:11:19Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 3, 2023, 8:11pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/1 "2023-10-03T20:11:19Z")

</div>

Ok so I id like to trigger a workflow on a variable change. How do I do this in its simplest sense?

@broberto @alexis @Joyce

Thanks in advance! Jon

---

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 3, 2023, 8:55pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/2 "2023-10-03T20:55:57Z")

</div>

So far Ive been trying out weweb fro about a month.

Its been great but other than gracious contributions from @Broberto and @dorilama, the support from anyone from Weweb directly has been lacking @Joyce @Alexis

Should be a simple Q&A here that Ive asked above… Im sure many users have had this same question so lets answer it once here and it will be available for everyone else in the forum here forever 🙂

---

<div class="post-metadata">

### Author: ![mark](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/mark/32/4163_2.png) [@mark](https://community.weweb.io/u/mark)
#### Post date: [October 3, 2023, 9:06pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/3 "2023-10-03T21:06:35Z")

</div>

I’d love to know if there’s a way to do this as well

---

<div class="post-metadata">

### Author: ![Joyce](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/joyce/32/13232_2.png) [@Joyce](https://community.weweb.io/u/Joyce)
#### Post date: [October 3, 2023, 9:23pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/4 "2023-10-03T21:23:13Z")

</div>

Hi @Jonny, I guess it would depend on:

- what type of variable is changing,
- how it’s changing, and
- what needs to happen when it changes

Can you give us more information about your use case?

---

<div class="post-metadata">

### Author: ![Joyce](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/joyce/32/13232_2.png) [@Joyce](https://community.weweb.io/u/Joyce)
#### Post date: [October 3, 2023, 9:30pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/5 "2023-10-03T21:30:40Z")

</div>

> [@Jonny](#):
>
> Its been great but other than gracious contributions from @Broberto and @dorilama, the support from anyone from Weweb directly has been lacking @Joyce @Alexis

That’s the beauty of the community! Lots of different users can contribute with their different expertise. @Broberto is a Supabase king for example and @dorilama my #1 custom code reference! 😀

When I see community users answering a topic, I avoid chiming in unless I think I can add something of value because I don’t want to disturb the flow of the conversation or create confusion.

From what I can see, you created 5 topics before this one, got answers for all 5 of them, and @Alexis and I contributed to 2 of those 5 topics so for me, it’s job done unless you tell me otherwise. Is it important for you that a WeWeb team member contributes to every discussion?

Would love your feedback on this so we can better understand how to make this community as useful and helpful as possible.

---

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 4, 2023, 6:54am UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/6 "2023-10-04T06:54:18Z")

</div>

@Joyce No its not important that a weweb member contributes to every single discussion, and though its the hot trend to not really offer support for software these days and have forums such as this, I think that it makes sense for those software companies to have a lot of topics answered and discussed already (for users to parse) or be scouring the threads quickly if one of the other champions happens to not get to them quickly.

I completely rebuilt my app on weweb from bubble in like 2 weeks and only had like 5 questions, so that speaks to how simple weweb really is. Its jsut the questions that I needed answered I come here, after about an hour of trouble shooting on my own, so it helps to know there is at least a moderately quick way of getting a response.

I didn’t know you were just letting them have at it, I was just hoping that someone from Weweb was looking at the forums hourly or so, as opposed to every 3 days which is what i thought lol 🙂

Anyways, essentially I am just trying to change a page based variables (a json variable) and then if that variable changes, run another workflow (the workflow can be anywhere in the app), i just need to trigger the workflow somehow to run based upon that variable change… It is this variable object and it will be updated from another workflow (user triggered)…

The variable is edited when the user completes the oauth flow, and at that point the variable changes, and that variable is the data i need to have in supabase. If I just continue with the workflow as is, it will run too quick…

 ![Screenshot 2023-10-04 at 1.51.46 AM](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/b/b6631867b680382c5dee55a54ce31cb97ce03bb0.png)

---

<div class="post-metadata">

### Author: ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)
#### Post date: [October 4, 2023, 7:44am UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/7 "2023-10-04T07:44:17Z")

</div>

Can’t you trigger the change on the end of oAuth flow? I see it very difficút as of now to listen to variable changes. Even tho the topic has already been asked here a few times.

There are some ways, like a custom component listening to changes, but that could come costly as you need a workspace plan for that.

You could recurringly listen for changes via a recursive js function, but that would be heavily resource innefective.

I think your best bet is to trigger the global workflow (that should be possible) on the end of your oAuth flow.

---

<div class="post-metadata">

### Author: ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)
#### Post date: [October 4, 2023, 7:50am UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/8 "2023-10-04T07:50:49Z")

</div>

I might be able to write a script for this with vue petite (6kb bundle size) but I’d need to know if there are any updates to this coming, so I dont waste time on something that gets a native patch in a few days. @flo

---

<div class="post-metadata">

### Author: ![Alexis](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/alexis/32/13248_2.png) [@Alexis](https://community.weweb.io/u/Alexis)
#### Post date: [October 4, 2023, 8:42am UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/9 "2023-10-04T08:42:39Z")

</div>

Hi, not possible in a nocode way at the moment.

The best is to create a global workflow doing the change and the flow you want to execute after the change and always use this global workflow to update the variable. A kind of getter/setter pattern.

---

<div class="post-metadata">

### Author: ![aurelie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/aurelie/32/13249_2.png) [@aurelie](https://community.weweb.io/u/aurelie)
#### Post date: [October 4, 2023, 9:23am UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/10 "2023-10-04T09:23:27Z")

</div>

We opened a ticket internally to think about this usecase (I personally want it for a lot of time), and have a nocode watch function. But it will probably not be treated before component.

I am wondering if we can just configure a watch with the Vue instance in the meantime.

Note: So i checked and

- The app instance `wwLib.getFrontWindow().vm` is available in both editor and published app. But it seems it does not expose the watch function
- The Vue instance `wwLib.getFrontWindow().Vue` is available only in the editor. I open a ticket to maybe as a quick workaround expose it in the publised app as well. It will let you access Vue.watch function then

---

<div class="post-metadata">

### Author: ![MichaelLovell](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/michaellovell/32/2157_2.png) [@MichaelLovell](https://community.weweb.io/u/MichaelLovell)
#### Post date: [October 4, 2023, 10:34am UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/11 "2023-10-04T10:34:14Z")

</div>

@Jonny not sure if this is helpful. I’ve included an example below where onLoad I’m waiting for a response code to be received, I’m checking for that response to not be empty every second with a limit of 8 attempts, if the response is received I’m updating a variable and executing a global workflow.

I’m using this pattern across a number of other areas and find the ‘while’ loop most effective. And i’m doing it for the same reason of delaying a call to Xano.

Depending how long you are ‘waiting’ for a user to complete the oauth flow, you could execute a similar script on the first user event and adjust the setTimeout value so that it gives reasonable time for them to complete the required steps.

```auto
<script>
console.log("Starting function turnstile")
function onloadTurnstileCallback() {
  turnstile.render(
    turnstile.execute("#cf-turnstile-container", {
      callback: function (token) {
        console.log(`Challenge Success ${token}`);

        const retryLimit = 8;
        let retryCount = 0;
        let response = "";

        function handleResponseAndRetry() {
        console.log("Starting handleResponseAndRetry")
          let response = turnstile.getResponse("#cf-turnstile-container");
          console.log(response);

          while (response === "" && retryCount < retryLimit) {
            retryCount++;
            console.log(`Empty response. Retrying (${retryCount}/${retryLimit})...`);
            setTimeout(handleResponseAndRetry, 1000); // Retry after 1 second
          }

          if (response === "" && retryCount === retryLimit) {
            console.log("Maximum retry attempts reached. Exiting...");
            return;
          }
          
          console.log(`Execute workflow 'cf-xanoCallback'. Retry count (${retryCount}/${retryLimit})...`);
          wwLib.wwVariable.updateValue("8a799d83-2049-4b21-a38c-6622265f38d4", response);
          wwLib.executeWorkflow("6b1605eb-6fb1-47dd-9ef4-0d5ad47cf919");
        }

        handleResponseAndRetry();
      },
    })
  )
}
</script>

```

---

<div class="post-metadata">

### Author: ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)
#### Post date: [October 4, 2023, 12:03pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/12 "2023-10-04T12:03:25Z")

</div>

That would be amazing, this would save me importing **vue petite** and doing this via their methods. That would be cool 🙂

---

<div class="post-metadata">

### Author: ![raydeck](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/raydeck/32/384_2.png) [@raydeck](https://community.weweb.io/u/raydeck)
#### Post date: [October 4, 2023, 12:23pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/13 "2023-10-04T12:23:11Z")

</div>

I whipped up a script composing that with executeWorkflow to address your use case:

> **[Scripttag.me](https://scripttag.statechange.ai/shared/4bb46e0b-89c9-4291-8d6b-d7912aa31437)**
>
> The easiest way to embed just enough custom javascript and CSS

The complicated part is subscribing to variable changes. I previously made a video on the core of this question that you might find of interest:

[![](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/8/862e80632d096d22701f5b81bcbe6cb54a9d4567.jpeg "Subscribing to Variable Changes in Weweb (@weweb\_io)") ](https://www.youtube.com/watch?v=IJkgBZKPxBc)

---

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 4, 2023, 6:21pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/14 "2023-10-04T18:21:53Z")

</div>

Lots of great stuff here @Broberto @raydeck @MichaelLovell @Alexis

Will try a few of them out and post back.

To address a few of the aforementioned thoughts, yes the primary use case (for me at least) is for an oauth flow within the app. The user clicks a button, opens a new tab and goes through the oauth flow.

They can take 1 minute for the oauth flow, but who knows, maybe they have the tab open an hour or finish it up the next day, so the “time based” oauth scenarios arent the best.

Essentially this would be an global “event listener” type of situation where once there is any oauth credential added, we catch it and I can then add it to xano/supabase wherever… @MichaelLovell @raydeck - I will try bot of the scripts mentioned in your posts and post back here soon!

Thanks again guys! Hopefully others get use out of this as well!

Jon

---

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 4, 2023, 8:10pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/15 "2023-10-04T20:10:23Z")

</div>

Hey guys, quick question… Would this be simpler If I didnt have any sort of weweb variable at all? I dont “need” a variable in weweb, I jsut need to catch the credentials after they finish oauth flow and then initiate the workflow based upon those…Would the variable in weweb being updated not be an added step which we wouldnt need? Thanks!

@raydeck @MichaelLovell

---

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 4, 2023, 9:32pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/16 "2023-10-04T21:32:04Z")

</div>

Howdy guys! @Broberto @raydeck @MichaelLovell

I have tried this script:

//Function to trigger callback when a specific weweb variable at a given ID (e.g. 1234-dba3-dffgg etc) changes  
const wwSubscribe = (key, callback) =\> {  
const window = wwLib.getFrontWindow();  
if (!window.\_\_subscribedValues) window.\_\_subscribedValues = {};  
window.\_\_subscribedValues[key] = wwLib.wwVariable.getValue(key);  
return wwLib.$store.subscribe((e, t) =\> {  
if (e.type == “data/setVariableValue”) {  
const v = t.data.variables[key];  
if (v && v.value !== window.\_\_subscribedValues[key]) {  
window.\_\_subscribedValues[key] = v.value;  
callback(v.value, key, window.\_\_subscribedValues[key]);  
}  
}  
});  
};

//Function to trigger workflows when a variable changes  
const triggerWorkflowFromSubscribe = (variableId, workflowId) =\> {  
wwSubscribe(variableId, (value, key) =\> {  
wwLib.executeWorkflow(workflowId, { value, key });  
});  
};

//Demo - let’s run the function to run the workflow when the variable changes  
triggerWorkflowFromSubscribe(“variables[‘80624cae-f0bd-4f72-9437-6e952fa5a173’]”, “d50090c7-ae47-4616-a6c2-aaac5249a357”);

I have inserted the variable to use as the “trigger” and the workflow ID to execute when this is supposed to run. It does run on page load, however, doesnt seem to be executing the workflow when the variable is changed… I have confirmed that the variable is changing, just not execution… Any flaws in the code that Ray made so graciously 🙂

thanks!

---

<div class="post-metadata">

### Author: ![raydeck](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/raydeck/32/384_2.png) [@raydeck](https://community.weweb.io/u/raydeck)
#### Post date: [October 4, 2023, 10:13pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/17 "2023-10-04T22:13:35Z")

</div>

There may be other issues, but triggerWorkflowFromSubscribe take the variable ID as its argument. e.g.

`triggerWorkflowFromSubscribe('80624cae-f0bd-4f72-9437-6e952fa5a173', "d50090c7-ae47-4616-a6c2-aaac5249a357");`

not

`triggerWorkflowFromSubscribe(“variables[‘80624cae-f0bd-4f72-9437-6e952fa5a173’]”, “d50090c7-ae47-4616-a6c2-aaac5249a357”);`

---

<div class="post-metadata">

### Author: ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://community.weweb.io/u/Jonny)
#### Post date: [October 4, 2023, 10:15pm UTC](https://community.weweb.io/t/trigger-workflow-on-variable-change/4717/18 "2023-10-04T22:15:17Z")

</div>

@raydeck That was it! Yeah I thought I could pass it in but I just went over to Dev editor and snagged it. Just tried it now (albeit only once) and it worked beautifully!
