# Ignition Button Script

**URL:** <https://forum.inductiveautomation.com/t/ignition-button-script/78654>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 1, 2023, 6:15am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654 "2023-09-01T06:15:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soundararajans\_S](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soundararajans\_S](https://forum.inductiveautomation.com/u/Soundararajans_S)\
**Post date:** [September 1, 2023, 6:15am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/1 "2023-09-01T06:15:34Z")

</div>

Hi,

below script is run when am press the button --\> onactionperformed. (image also attached)

if assetnumber == '':  
system.perspective.openPopup('fillerror1', 'Popup/machinemaster/fillerror', title = 'Fill all!!!')  
self.getSibling("Label\_5").props.text = '1'  
elif data[0]['assetnumber'] != '':  
system.perspective.openPopup('machineexist', 'Popup/machinemaster/machineexist', title = 'Asset Already Exist!!!')  
self.getSibling("Label\_5").props.text = '2'  
elif assetnumber == 'ABCD':   
self.getSibling("Label\_5").props.text = '3'  
system.db.runNamedQuery("Master/machine/createnew", {'assetnumber':assetnumber, 'machinename':machinename, 'process':process, 'capacity':capacity, 'validity':validity, 'location':location, 'modifiedon':modifiedon})  
else:  
print ''

In this if and first elif is working fine. 2nd elif is not working. What is the problem in this code?  
label\_5 is getting 1 & 2. But 3 is not writing. Asset number given as 'ABCD'. still not working.

 ![1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/a/3ad106295b1032b412a87e9c4085c81ef0059804.png)

---

<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:** [September 1, 2023, 6:31am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/2 "2023-09-01T06:31:28Z")

</div>

Change the print statement of the last else to this:

```auto
print assetnumber
print type(assetnumber)

```

...and see what the output is.

Also: if you put your code between two sets of thee back ticks it will render correctly in the forum.  
**Example:**  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/8/d847497b9966e5e4c13efe2ad6e80c23edf443aa.png)

---

<div class="post-metadata">

**Author:** ![Soundararajans\_S](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soundararajans\_S](https://forum.inductiveautomation.com/u/Soundararajans_S)\
**Post date:** [September 1, 2023, 6:40am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/3 "2023-09-01T06:40:33Z")

</div>

No. It's not working. Problem is 2nd elif is not working, when I give asset number as 'ABCD'.

First if and elif is working fine.

---

<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:** [September 1, 2023, 6:47am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/4 "2023-09-01T06:47:07Z")

</div>

I noticed this is perspective, so you would need to use [system.perspective.print](https://docs.inductiveautomation.com/display/DOC81/system.perspective.print)

In any case, I understand that the second `elif` isn't working, but perhaps I need you to be more specific. By not working, do you mean that the `else` statement is running when the second `elif` fails, or is something in the second `elif` failing?

Is there any chance that `data[0]['assetnumber']` is not equal to `''` when `assetnumber` is equal to `'ABCD'`? If so, then the first `elif` will evaluate as `True`, and the second `elif` will never run its code.

---

<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:** [September 1, 2023, 6:50am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/5 "2023-09-01T06:50:41Z")

</div>

Perhaps rewrite it like this:

```auto
if assetnumber == '':
	system.perspective.openPopup('fillerror1', 'Popup/machinemaster/fillerror', title = 'Fill all!!!')
	self.getSibling("Label_5").props.text = '1'
elif data[0]['assetnumber'] != '' and assetnumber != 'ABCD':
	system.perspective.openPopup('machineexist', 'Popup/machinemaster/machineexist', title = 'Asset Already Exist!!!')
	self.getSibling("Label_5").props.text = '2'
elif assetnumber == 'ABCD':
	self.getSibling("Label_5").props.text = '3'
	system.db.runNamedQuery("Master/machine/createnew", {'assetnumber':assetnumber, 'machinename':machinename, 'process':process, 'capacity':capacity, 'validity':validity, 'location':location, 'modifiedon':modifiedon})
else:
	pass

```

---

<div class="post-metadata">

**Author:** ![Soundararajans\_S](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soundararajans\_S](https://forum.inductiveautomation.com/u/Soundararajans_S)\
**Post date:** [September 1, 2023, 7:12am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/6 "2023-09-01T07:12:05Z")

</div>

Same. It's not working.

Below is my full code

```auto
	partnumber = self.getSibling("partnumentry").props.text
	partdes = self.getSibling("partdesentry").props.text
	modifiedon = system.date.now()
	createdby = self.getSibling("createdby").props.text
	
	data = system.db.runNamedQuery("Master/partmaster/partcheck", {"partnumber":partnumber})
	self.getSibling("Label_2").props.text = '60'
	if data[0]['partnumber'] != '':
		system.perspective.openPopup("partexist1",'Popup/partmaster/partexist', title = 'Part already exist')
	elif partnumber == '' or partdes == '' or createdby == '':
		system.perspective.openPopup("fillerror",'Popup/partmaster/fillerror')
	else:
		system.db.runNamedQuery("Master/partmaster/pminsert", {"partnumber":partnumber, "partdes":partdes, "modifiedon":modifiedon, "createdby":createdby})

```

here 'data' variable used to select data from SQL table. When selecting, 'data' variable is empty result is getting like this. From the result am taking 'data[0]['partnumber']'. If data variable is empty, I think am facing this issue. Is it correct?  
Any option to avoid this issue?

---

<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:** [September 1, 2023, 7:19am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/7 "2023-09-01T07:19:59Z")

</div>

> [@Soundararajans\_S](#):
>
> Any option to avoid this issue?

Make sure the data you're trying to access actually exists.  
You can just add a check like this:

```python
if not data:
    # handle empty data

```

That being said, `runNamedQuery` returns a dataset. You can't use the `[]` operator on a dataset.  
You'll need to either use the built-in dataset handling functions/methods, or convert it to a `pyDataSet`.

disclaimer: Maybe that changed in more recent versions, but I'm stuck on 8.1.17 and I'm too lazy to run a sandbox just to test this :X

---

<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:** [September 1, 2023, 7:47am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/8 "2023-09-01T07:47:47Z")

</div>

> [@pascal.fragnoud](#):
>
> `runNamedQuery` returns a dataset.

Perhaps what is needed is something like this:

```auto
if data and data.getValueAt(0, 'partnumber') != '':

```

---

<div class="post-metadata">

**Author:** ![Soundararajans\_S](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Soundararajans\_S](https://forum.inductiveautomation.com/u/Soundararajans_S)\
**Post date:** [September 1, 2023, 8:23am UTC](https://forum.inductiveautomation.com/t/ignition-button-script/78654/9 "2023-09-01T08:23:58Z")

</div>

given code is working.

Thank You.
