# Trouble with 'if' statement, button event

**URL:** <https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 25, 2023, 5:49pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892 "2023-07-25T17:49:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 25, 2023, 5:49pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/1 "2023-07-25T17:49:50Z")

</div>

For some reason the code will not run past line 37.

First step: assign values to variables.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/5/e5fc955a76e82d676c9e92508728201153783890.png)

Second step: assign those values to labels to make sure I'm getting them:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/7/172ad8341e7010c3865498f11686f0c956a92204.png)

Third step: check for missing data:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/0/008f50779b149f5410887aa54366e106874910cb.png)

All that works fine.

Next, check for a value of zero:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/3/c3c715f980b6546e63e64139f2f1c69544d2caa2.png)

And this is where the code seems to stop, it won't execute this line or the `else:` later on.  
_EDIT: It does execute the `else:` statement._

_For some reason `item_id` does not equal 0._

I have commented out the other `IF` statements, no change. No errors show on the side of the code window, no errors in the gateway log.

Any ideas on what would cause code to just stop without any indications? The zero (0, not O) value is a zero, triple checked that.

Thanks

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 25, 2023, 5:55pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/2 "2023-07-25T17:55:38Z")

</div>

Can you just post your entire code? With Python's sensitivity to whitespace, there's a whole class of errors we can't rule out. Also, we're taking your word for the line numbers.

Are you sure the _type_ lines up with what you expect? A _string_ `"0"` would not equal `0` the int/float.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 25, 2023, 5:57pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/3 "2023-07-25T17:57:36Z")

</div>

```auto
	item_id = self.view.params.p_itemID
	
	desc = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("txtA_Description").props.text
	notes = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("txtA_Notes").props.text
	classification = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("cbo_Classification").props.value
	category = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("cbo_Category").props.value
	stocked = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("chk_Stocked").props.selected
	review = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("chk_Review").props.selected
	shortDesc = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("txt_ShortDescription").props.text
	
	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label_0").props.text = "Item ID: " + item_id
	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label_1").props.text = "Category: " + str(category)
	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label_2").props.text = "Class: " + str(classification)
	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label_3").props.text = "Desc: " + desc
	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label_4").props.text = "Short: " + shortDesc
	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label_5").props.text = "Stocked: " + str(stocked)
	
#	msg = "step one"
#	self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label").props.text = msg
	
	if category == 0:
		param2 = {"p_Msg":"Please select a Category."}
		system.perspective.openPopup("popup102", "Page/Message_PU", param2)
		return
	if classification == 0:
		param2 = {"p_Msg":"Please select a Class."}
		system.perspective.openPopup("popup102", "Page/Message_PU", param2)
		return
	if len(desc) == 0:
		param2 = {"p_Msg":"Please enter a description."}
		system.perspective.openPopup("popup102", "Page/Message_PU", param2)
		return
	if len(shortDesc) == 0:
		param2 = {"p_Msg":"Please enter a short description."}
		system.perspective.openPopup("popup102", "Page/Message_PU", param2)
		return
		
	if item_id == 0:
		param2 = {"p_Msg":"IF."}
		system.perspective.openPopup("popup102", "Page/Message_PU", param2)
# #Add New Item
# msg = "step two"
# self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Right").getChild("Label").props.text = msg
# if self.view.custom.currentTabIndex == 0:
# # General Tab
# param1 = {"p_Category":category, "p_Classification":classification, "p_Description":desc, "p_ShortDesc":shortDesc, "p_Stock":stocked, "p_Review":review, "p_Notes":notes}
# namedQuery = "INV/Item_NewInsert_SP"
# returnNQ = system.dataset.toPyDataSet(system.db.runNamedQuery(namedQuery, param1))
# self.parent.parent.getChild("flx_Title").getChild("lbl_ItemID2").props.text = returnNQ[0][0]
# self.parent.parent.getChild("flx_Title").getChild("lbl_Desc").props.text = shortDesc
# self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("num_Catalog").props.value = returnNQ[0][1]
## self.parent.parent.getChild("TabContainer").props.currentTabIndex = 1
# elif self.view.custom.currentTabIndex == 1:
# # Stocking data
# item_id2 = self.view.params.p_itemID
# low = self.parent.parent.getChild("TabContainer").getChild("flx_Stock").getChild("coord_Left").getChild("num_LowStock").props.value
# high = self.parent.parent.getChild("TabContainer").getChild("flx_Stock").getChild("coord_Left").getChild("num_HighStock").props.value
# current = self.parent.parent.getChild("TabContainer").getChild("flx_Stock").getChild("coord_Left").getChild("num_CurrentStock").props.value
# safety = self.parent.parent.getChild("TabContainer").getChild("flx_Stock").getChild("coord_Left").getChild("num_SafetyStock").props.value
# over = self.parent.parent.getChild("TabContainer").getChild("flx_Stock").getChild("coord_Left").getChild("num_OverStock").props.value
# bin = self.parent.parent.getChild("TabContainer").getChild("flx_Stock").getChild("coord_Left").getChild("cbo_Bin").props.value
# param2 = {"p_ItemID":item_id2, "p_BinID":bin, "p_Low":low, "p_High":high, "p_Current":current, "p_Safety":safety, "p_Over":over}
# namedQuery2 = "INV/Bin_Item_Insert_SP"
# system.db.runNamedQuery(namedQuery2, param2)
# message = "inv_Bin_Tbl_Refresh"
# system.perspective.sendMessage(message)
## self.parent.parent.getChild("TabContainer").props.currentTabIndex = 3
# elif self.view.custom.currentTabIndex == 3:
# # Vendor data
# item_id2 = self.view.params.p_itemID
# vendor = self.parent.parent.getChild("TabContainer").getChild("flx_Vendors").getChild("coord_Left").getChild("cbo_Vendor").props.value
# container = self.parent.parent.getChild("TabContainer").getChild("flx_Vendors").getChild("coord_Left").getChild("cbo_Container").props.value
# partNum = self.parent.parent.getChild("TabContainer").getChild("flx_Vendors").getChild("coord_Left").getChild("txt_PartNumber").props.text
# price = self.parent.parent.getChild("TabContainer").getChild("flx_Vendors").getChild("coord_Left").getChild("num_Price").props.value
# lead = self.parent.parent.getChild("TabContainer").getChild("flx_Vendors").getChild("coord_Left").getChild("num_LeadTime").props.value
# param3 = {"p_ItemID":item_id2, "p_Vendor":vendor, "p_PartNum":partNum, "p_Price":price, "p_ContainerID":container, "p_Lead":lead}
# namedQuery3 = "INV/Item_Supplier_Insert_SP"
# system.db.runNamedQuery(namedQuery3)
# message = "inv_Pn_Tbl_Refresh"
# system.perspective.sendMessage(message)
	else:
		param2 = {"p_Msg":"ELSE."}
		system.perspective.openPopup("popup102", "Page/Message_PU", param2)
# # Edit/Update existing Item
## desc = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("txtA_Description").props.text
## notes = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("txtA_Notes").props.text
## classification = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("cbo_Classification").props.value
## category = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("cbo_Category").props.value
## stocked = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("chk_Stocked").props.selected
## review = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("chk_Review").props.selected
# disc = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("chk_Obsolete").props.selected
# replaced_by = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("num_ReplacedBy").props.value
## shortDesc = self.parent.parent.getChild("TabContainer").getChild("flx_General").getChild("coord_Left").getChild("txt_ShortDescription").props.text
# query1 = "INV/Item_Details_Update"
# params = {"p_ItemID":item_id, "p_Class":classification, "p_Category":category, "p_Desc":desc, "p_Short":shortDesc, "p_Stocked":stocked, "p_Review":review, "p_Disc":disc, "p_Notes":notes, "p_ReplacedBy":replaced_by}
# system.db.runNamedQuery(query1, params)
# message = "inv_tbl_Refresh"
# system.perspective.sendMessage(message)

```

The View Param:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/8/986129d20f2cb2414a27b9ff96323121bb7719cd.png)

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 25, 2023, 6:01pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/4 "2023-07-25T18:01:03Z")

</div>

Changed second line to this:

```auto
item_id = int(self.view.params.p_itemID)

```

And it works. I don't get it. If I type an "O" in for the view param, I get errors on the components bound to it, but the code somehow interprets it as a zero string "0".

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 25, 2023, 7:32pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/5 "2023-07-25T19:32:10Z")

</div>

I don't know what's going on with the parameter problem but I offer the following to reduce your code and the brittleness of your project.

1. On the view create a custom property object with structure something like this:

```auto
dataLeft
    desc :
    notes :
    classification :
    category :
    stocked :
    review :
    shortDesc :

```

1. Bind each of the respective `coord_Left` components to the custom object and make the bindings bi-directional.
2. Now the first block of seven lines of your code becomes,

```auto
dataLeft = self.view.custom.dataLeft

```

1. Create a second custom property object, `dataRight`.
2. Generate the `coord_Right` components using:

```auto
dataRight = {}
dataRight["Item ID"] = item_id
dataRight["Category"] = str(dataLeft["category"])
dataRight["Class"] = str(dataLeft["classification"])
dataRight["Desc"] = desc[quote="mdemaris, post:3, topic:76892"] # What's going on here?
dataRight["Short"] = dataLeft["shortDesc"]
dataRight["Stocked"] = str(dataLeft["stocked"])
self.view.custom.dataRight = dataRight

```

1. Bind all the coord\_Right components to the custom parameter object members.

* * *

I suspect that most of this routine could be eliminated by disabling the button that calls this event until all the relevant fields have been filled out. Show a red `*` or icon beside each field until it's got a legitimate value and provide a tooltip on the icon to indicate what the problem is.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 25, 2023, 8:11pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/6 "2023-07-25T20:11:35Z")

</div>

> [@Transistor](#):
>
> Show a red `*` or icon beside each field until it's got a legitimate value and provide a tooltip on the icon to indicate what the problem is.

How would I show a red symbol or icon?

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 25, 2023, 8:13pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/7 "2023-07-25T20:13:59Z")

</div>

> [@Transistor](#):
>
> make the bindings bi-directional.

Why would I want to make these bindings bi-directional?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 25, 2023, 8:23pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/8 "2023-07-25T20:23:18Z")

</div>

> [@mdemaris](#):
>
> How would I show a red symbol or icon?

- Add a label or an icon component after each form field.
- Add an expression binding to the icon's visibility property.
- Have the expression check that there is enough information in the field.
- Then you could create an expression binding on the button's enabled property to check that all the fields are filled out. Something like,

```auto
!{../txtA_DescIcon.visible} && 
!{../txtA_ClassIcon.visible} && 
!{etc., etc.}

```

If none of the error icons are visible then your form is ready and the submit button can be enabled.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 25, 2023, 10:42pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/9 "2023-07-25T22:42:54Z")

</div>

> [@mdemaris](#):
>
> Why would I want to make these bindings bi-directional?

So that they update when you edit your view.  
If the bindings are not bidirectional then they are read-only as far as the bound component is concerned.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 28, 2023, 10:29pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/10 "2023-07-28T22:29:02Z")

</div>

Found something interesting on this parameter issue:  
in the designer, if I use an expression binding and say `if param = 0`, numeric zero, it comes back true. But, when I launch a perspective session, I have to use `if (toint(param) = 0)` for it to come back true.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 28, 2023, 10:34pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/11 "2023-07-28T22:34:28Z")

</div>

When you enter a property value in the text field in the designer, it automatically checks to see if it looks numeric (i.e. is just `0`) and sets it into the property _as a number_.

In the actual browser session, that's not happening automatically, so if you're e.g. setting a custom property to the output of a text field, you're getting a _string_ `"0"` instead of the number `0`.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 28, 2023, 10:36pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/12 "2023-07-28T22:36:34Z")

</div>

You can simplify that to  
`if(!toInt(param), true, false)`

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 28, 2023, 10:37pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/13 "2023-07-28T22:37:15Z")

</div>

You can simplify _that_ to `!toInt(param)` 🙂

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 28, 2023, 10:37pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/14 "2023-07-28T22:37:58Z")

</div>

> [@paul-griffith](#):
>
> ou can simplify _that_ to `!toInt(param)`

Time for bed!

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 28, 2023, 10:38pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/15 "2023-07-28T22:38:27Z")

</div>

This checks to see if it is an integer, no?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 28, 2023, 10:41pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/16 "2023-07-28T22:41:45Z")

</div>

`toInt(param)` will return **true** for any non-zero value of param.  
`!toInt(param)` will return **false** for any non-zero value of param.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 28, 2023, 10:42pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/17 "2023-07-28T22:42:58Z")

</div>

That is - expressions must always return a value.  
If you're in a _context_ where the output of an expression is required to be a boolean, type coercion will be performed. A non-zero integer -\> true; anything else -\> false.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 28, 2023, 10:45pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/18 "2023-07-28T22:45:37Z")

</div>

If `view.param.p1` (for instance) is equal to numeric 0, then several things happen, if it is larger than 0, other things happen. The value for `p1` is passed from a calling event on another view.

In each script so far, I have had to use `toint()` or `int()`, depending on the type of script/language used, except in the Designer.

I don't think I have any code that changes `p1` to 0, only code that changes it to a larger value.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 28, 2023, 10:52pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/19 "2023-07-28T22:52:21Z")

</div>

What initially sets `view.param.p1`?

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 28, 2023, 10:57pm UTC](https://forum.inductiveautomation.com/t/trouble-with-if-statement-button-event/76892/20 "2023-07-28T22:57:16Z")

</div>

Call from a table row click:

```auto
system.perspective.openPopup(popId, popView1, params={"p_itemID":self.props.selection.data[0].ItemID, "pu_id":popId, "p_newItem":0}, showCloseIcon = True, resizable = True)

```

Where the data is pulled from a named query, from SQL SVR, where this `ItemID` is an auto-increment primary key integer. BUT, the zero is not from the query... but is hard coded as you see for the `p_newItem`.

I agree with Transistor, I need a nap! Three edits to make this reply sensible...
