# Script on mouseClicked Button requires 2 clicks to run

**URL:** <https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668>\
**Category:** Ignition\
**Tags:** ignition81, database, vision, scripting\
**Created:** [January 17, 2023, 3:02pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668 "2023-01-17T15:02:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 3:02pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/1 "2023-01-17T15:02:49Z")

</div>

Hello all!

I have a script that is supposed to read the selected row, grab a cell based on the column header I give, and then run a named query to update the database using that cells data as a parameter.

The script will run fine, although it doesn't seem to fire until I click it twice. I have tried changing the event from a mouseReleased to mouseClicked and still have the same issue. I will paste the script below; it is pulling the data from a Power Table. The table pulls from a named query.

Script:

```auto
table = event.source.parent.getComponent('Power Table')
 
selectedIndices = table.getSelectedRows()
 
if len(selectedIndices) > 0:
     
    for index in selectedIndices:
     
        taskId = table.data.getValueAt(index, "task_id1")   
     
      	system.tag.writeBlocking("[default]placeholder", taskId)
      	
      	params = {"TID":event.source.parent.getComponent('Label 5').text}
      	
      	system.db.runNamedQuery("Complete Task", params)
      	
else:
	print "oops!"

```

Please do let me know if I need any more information, thank you!

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 17, 2023, 3:16pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/2 "2023-01-17T15:16:50Z")

</div>

Don't run this on the mouse click because the event fires before the index is selected. Run this from the propertyChange event handler with a selectedIndex qualifier.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 17, 2023, 3:18pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/3 "2023-01-17T15:18:07Z")

</div>

> [@Drop down, change of selection](https://forum.inductiveautomation.com/t/drop-down-change-of-selection/68142/5):
>
> If you are attempting this from the mouse clicked event handler, the problem is that the event fires before the index is selected. This is why it doesn't work on the first click but does work on the second. Of course, by the second click, all of the other scripting is out of sequence. The correct way to fire this script is from the propertyChange event handler. Simply take all of your code and nest it in the following if statement: if event.propertyName == 'selectedIndex': [image]

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 3:33pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/4 "2023-01-17T15:33:01Z")

</div>

Thank you!

Although, it seems to be doing the same thing. Here is the script as it stands now.

```auto
if event.propertyName == 'selectedIndex':
	# Grab a reference to the table.
	table = event.source.parent.getComponent('Power Table')
	 
	selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
	if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1")   
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	system.tag.writeBlocking("[default]placeholder", taskId)
	     
	      	params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	      	system.db.runNamedQuery("Complete Task", params)
	      	
	else:
		print "oops!"

```

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 17, 2023, 3:35pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/5 "2023-01-17T15:35:48Z")

</div>

Is this being fired from the table, or some other component?

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 3:37pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/6 "2023-01-17T15:37:02Z")

</div>

It is being fired from a button! I apologize for not stating that earlier.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 17, 2023, 3:37pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/7 "2023-01-17T15:37:56Z")

</div>

Then use the actionPerformed event handler with no qualifier.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 3:42pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/8 "2023-01-17T15:42:59Z")

</div>

Thank you, would I need to use the event object property `source`?

I tried running it in the actionPerformed as well but same result. I did not use `if event.propertyName == 'selectedIndex':` as well

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 17, 2023, 3:50pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/9 "2023-01-17T15:50:59Z")

</div>

I don't see any reason why this wouldn't work on the first click from the actionPerformed event handler.

```auto
table = event.source.parent.getComponent('Power Table')
	 
	selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
	if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1")   
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	system.tag.writeBlocking("[default]placeholder", taskId)
	     
	      	params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	      	system.db.runNamedQuery("Complete Task", params)
	      	
	else:
		print "oops!"

```

Add print statements after each variable to show their values. Perhaps it will shed some light on what's going on.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 17, 2023, 3:54pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/10 "2023-01-17T15:54:54Z")

</div>

How do you know it isn't working on the first click? It writes to a tag and it causes a query to run. (An update query of some kind, presumable, as you are discarding the return value.) Are you monitoring the tag? Are you querying the database elsewhere to see the DB changes?

Such a script commonly included a `system.db.refresh(....)` operation to cause a table or graph to pick up the DB changes.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 4:03pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/11 "2023-01-17T16:03:27Z")

</div>

I am checking within the DB and with one click it does not update the database as well. I also have the query tied to the "data" component on the power table that updates every second for testing purposes. It does write to the memory tag on first click as well as display my `print` statements. Seems to always get stuck on the named query.

Here is my script as it stands now, I have added in print statements and teh `system.db.refresh` operation. This is still within the actionPerformed event.

```auto

	# Grab a reference to the table.
table = event.source.parent.getComponent('Power Table')
	 
selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1") 
	        print taskId  
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	task = system.tag.write("[default]placeholder", taskId)
	     	print task
	    params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	    system.db.runNamedQuery("Complete Task", params)
	      	
else:
		print "oops!"
		
system.db.refresh(table, "data")

```

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 4:05pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/12 "2023-01-17T16:05:06Z")

</div>

I have added print statemtents now which all display correctly on first click. Seems to be getting caught up on the named query. Here is my script with the print and refresh operations.

```auto
# Grab a reference to the table.
table = event.source.parent.getComponent('Power Table')
	 
selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1") 
	        print taskId  
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	task = system.tag.write("[default]placeholder", taskId)
	     	print task
	    params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	    system.db.runNamedQuery("Complete Task", params)
	      	
else:
		print "oops!"
		
system.db.refresh(table, "data")

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 17, 2023, 5:10pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/13 "2023-01-17T17:10:28Z")

</div>

Are you sure you need a second click, or is it just that it actually needs some time to do what it's supposed to do ?

You're doing tag writes and queries in a loop, which might be a bad idea.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [January 17, 2023, 5:24pm UTC](https://forum.inductiveautomation.com/t/script-on-mouseclicked-button-requires-2-clicks-to-run/68668/14 "2023-01-17T17:24:11Z")

</div>

Yes, I agree this isn't the best approach. Although, I was able to figure out a solution!

It seems rather than getting caught up on the named query, it may have something to do with the memory tag. I removed the tag write completely and used the component itself to grab the data I needed. This in turn allowed me to perform the script on a single click.

I may have been asking too much for the script to handle in a timely manner.
