# Query run if tag is active

**URL:** <https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165>\
**Category:** Ignition\
**Created:** [March 16, 2023, 2:16pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165 "2023-03-16T14:16:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 2:16pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/1 "2023-03-16T14:16:14Z")

</div>

how can i run query after my tag will active?  
i am trying to run a query once my tag active. i make it run by using toggle button  
( if event.stateChange == event.SELECTED:  
system.db.runUpdateQuery("INSERT INTO demo.line\_4\_data (line, date, days, starttime, endtime, reason)" + " VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" %(p, curday, x, startTime, endTime, reason), getKey=1)  
)

it is working when window is open but if i open another window its not working.  
is it because i put that query in component? can component script run only if component window is open?

if yes thn how i can make it dynamic so that query can run always based on tag active?

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [March 16, 2023, 2:24pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/2 "2023-03-16T14:24:31Z")

</div>

Add a value change script to the tag configuration. It sounds like the script lives on a component on a window/view, so that's why it is only executing when the window/view is open.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 16, 2023, 2:25pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/3 "2023-03-16T14:25:47Z")

</div>

Do you want the query to run once on the leading edge, poll at some rate? Is this query dependent on any client provided information? If multiple clients are open to the same window should they insert different data?

You window and your using `event` in your code, so that make me think this is Vision, is that the case?

As a side note, it's recommended to use `system.db.runPrepUpdate()` especially if you are accepting any user enter information as a parameter.

Also, please post code using the preformatted text option `</>`.

```auto
if event.stateChange == event.SELECTED:
    system.db.runUpdateQeury("INSERT INTO demo.line_4_data (line,date,days,starttime,endtime,reason) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" %(p, curday, x, startTime, endTime, reason), getKey=1)

```

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 3:14pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/4 "2023-03-16T15:14:17Z")

</div>

its like auto generated query. if tag is active the script query will run

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 3:17pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/5 "2023-03-16T15:17:41Z")

</div>

i did value changed script, no luck

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/9069812d508d9d563ce4d3fe327118a3913bc0d7.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 16, 2023, 3:23pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/6 "2023-03-16T15:23:29Z")

</div>

> [@lrose](#):
>
> Also, please post code using the preformatted text option `</>`.

👆

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [March 16, 2023, 3:30pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/7 "2023-03-16T15:30:25Z")

</div>

Check your gateway logs for script error

Status/Diagnostics/Logs

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 3:35pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/8 "2023-03-16T15:35:48Z")

</div>

```auto
system.db.runUpdateQuery("INSERT INTO demo.line_4_data (line, date, days, starttime, endtime, reason)" + " VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" %(p, curday, x, startTime, endTime, reason), getKey=1)

```

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 3:49pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/9 "2023-03-16T15:49:17Z")

</div>

not getting any log error related this

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [March 16, 2023, 3:57pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/10 "2023-03-16T15:57:58Z")

</div>

I see you're grabbing some dates off the UI. You'll need to connect those to tags and use `system.tag.readBlocking()` to get those values into your script.

You almost certainly should be getting an error because "event" is not defined.

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 4:08pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/11 "2023-03-16T16:08:50Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/90e5d9242b9131a29b55928455de2947f59048d3.png)  
i dont know this came 45 min before now , but i triggerd tag so many time afrer to check but error isnot coming, and value changed scrip also not working

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [March 16, 2023, 4:24pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/12 "2023-03-16T16:24:29Z")

</div>

It will suppress duplicate errors to avoid clogging up the log. That's your error. You'll need to rework your script to run in the gateway scope with the tags on the UI page in question.

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 7:21pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/13 "2023-03-16T19:21:32Z")

</div>

i created named Query and put in value changed and it worked. i dint know why it was not running through system.db.runUpdatequery or with updatePrep but works with name query

Thanks guys for help

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 16, 2023, 7:24pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/14 "2023-03-16T19:24:04Z")

</div>

runUpdateQuery() and runPrepUpdate() need a db connection name if it's running from the gateway scope.  
I've gotten into the habit of _always_ specifying the db connection for both consistency and robustness.

---

<div class="post-metadata">

**Author:** ![Parth\_Pandya](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Parth\_Pandya](https://forum.inductiveautomation.com/u/Parth_Pandya)\
**Post date:** [March 16, 2023, 7:37pm UTC](https://forum.inductiveautomation.com/t/query-run-if-tag-is-active/71165/15 "2023-03-16T19:37:42Z")

</div>

oh thats true i did not try with db nam in runUpdateQuery() and runPrepUpdate(). i will try that too  
thanks Jordan
