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.
Change the print statement of the last else to this:
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:

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.
I noticed this is perspective, so you would need to use 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.
Perhaps rewrite it like this:
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
Same. It's not working.
Below is my full code
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?
Make sure the data you're trying to access actually exists.
You can just add a check like this:
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
Perhaps what is needed is something like this:
if data and data.getValueAt(0, 'partnumber') != '':