Data Input to Database or Custom Session Prop on Button Press Not Working

I have a page where users will input information from dropdowns, I am trying to either set that information to a custom session prop and/or insert the input information into an sql database on a button press using the event configuration. I have used similar codes for different purposes but when I click the button after launching perspective neither option is working.

def runAction(self, event):
	date_time = self.getSibling("DateTimeInput").props.value
	work_order = self.getSibling("Dropdown_WorkOrder").props.value
	machine_status = self.getSibling("Dropdown_MachineStatus").props.value
	status_reason = self.getSibling("Dropdown_StatusReason").props.value
	planned_workplace = concat(self.getSibling("Dropdown_Location").props.value, self.getSibling("TextField_Machine").props.text)
	
	session.custom.date_time = date_time
	session.custom.work_order = work_order
	session.custom.machine_status = machine_status
	session.custom.status_reason = status_reason
	session.custom.planned_workplace = planned_workplace

	# Create a python dictionary of parameters to pass
	parameters = {"date_time":self.getSibling("DateTimeInput").props.value, 
					"work_order":self.getSibling("Dropdown_WorkOrder").props.value,
					"machine_status":self.getSibling("Dropdown_MachineStatus").props.value,
					"status_reason":self.getSibling("Dropdown_StatusReason").props.value,
					"planned_workplace":concat(self.getSibling("Dropdown_Location").props.value, self.getSibling("TextField_Machine").props.text)}
	
	# Run the Named Query
	system.db.runNamedQuery("Insert Status Data", parameters)

I'm not a Python expert but as far as I know concat() is for merging arrays. You just need the + operator:
planned_workplace = self.getSibling("Dropdown_Location").props.value + self.getSibling("TextField_Machine").props.text

Regarding the SQL query, have you set the default database in the project settings?

I note than you have two events on the one button, onClick and onActionPerformed.

You should run both session props update and SQL query on the onActionPerformed event for a couple of reasons.

  • onClick will execute even if the button is disabled. It may not bother you in this application but will catch you out sometime if you develop a bad habit.
  • onActionPerformed doesn't execute if the button is disabled. If I remember correctly, it will also work with keyboard action.

There should be errors in the gateway logs at Status | Diagnostics | Logs.

I was testing if either onClick or onActionPerformed would work, and neither of them work

You're not really giving us anything to go on. Give us information on:

  • What were you expecting?
  • What did you get?
  • What errors were generated? (Did you check on the gateway | Status | Diagnostics | Logs?)

onActionPerformed is the correct event.

I'm not sure what you mean by "neither of them work". I assume that means you didn't see the data show up in the database as expected.

Generally this means that something is happening that you're not expecting, most likely an error. I would expect you to get a pop up with an error message.

Now, on to what I think the issue actually is here, which @transistor hinted at earlier.

concat() isn't a built-in function, and I don't see any imports. I strongly susspect that you're getting an error, something like:

Error running action 'component.onActionPerformed' on Main Views/Trending/customTrend@D/root/Button: Traceback (most recent call last): File "<function:runAction>", line 2, in runAction NameError: global name 'concat' is not defined

and that script execution is never arriving at the second parts of your script.

You really just need the + operator to concatenate two strings.

I think probably what happened is you've mixed the Expression Language concat() expression with a jython script.

I would like for when the button is clicked for the information from the drop downs to be saved in a way that can be used in other views, I would prefer for it to be saved into the database, but also tried just setting them to a custom session prop.


I get this error popping up saying NoneType object has no attribute props, but I have a value selected for each dropdown
image
image
I did test the INSERT Named Query so it should work, it seems to be the button event code
I made the change to + and there is still an issue

def runAction(self, event):
	# Create a python dictionary of parameters to pass
	parameters = {"date_time":self.getSibling("DateTimeInput").props.value, 
				"work_order":self.getSibling("Dropdown_WorkOrder").props.value,
				"machine_status":self.getSibling("Dropdown_MachineStatus").props.value,
				"status_reason":self.getSibling("Dropdown_StatusReason").props.value,
				"planned_workplace":self.getSibling("Dropdown_Location").props.value + self.getSibling("TextField_Machine").props.text}
	
	# Run the Named Query
	system.db.runNamedQuery("Insert Status Data", parameters)"

Something is wrong with the name of the DateTimeInput. That error means that the self.getSibling() function is not finding the supplied component name. Usually that means that the spelling doesn't match. From looking at it, everything appears to be correct.

Make sure you haven't accidentally added a space to the end of the name.

2 Likes

Thank you, it was a silly mistake in the end. I had switch the textfield to a drop down but hadn't updated that in the event configuration code

You can avoid some of the getSibling coding - which tends to make your application "brittle" when you make changes - by binding your form elements to view custom properties. Then reference those in your scripts. You are then free to move and rename the components without breaking the relationships.

If you do this then form validation becomes much simpler too. You can have another custom property which gives a true if all the fields are OK. This in turn can be used to enable / disable the "Save" button, or whatever.