Insert Results of Named Query to Text Field

Hello all,

I think this one should be easy but I'm a bit stumped.

I need to use a button to fire a named query which will return only one result and insert that result into a text field. I do not want to bind the text property of the text field to the named query because I want to control when it runs, which is why I am adding the script to the button.

Here is the query:

SELECT cartID
FROM CommTrackCarts
WHERE   startBuild = (
	SELECT MAX(startBuild) FROM CommTrackCarts
	WHERE cartNumber =  :cartNumberParam )

And here is the script inside of my button:

params = {
	"cartNumberParam": event.source.parent.getComponent('Cart').text
}

system.db.runNamedQuery("INSERT Cart", params)

event.source.parent.getComponent('CartID').text = str(system.db.runNamedQuery("SELECT MAX", params))

Right now, this places the dataset into the text field and I need it to return a specific item from the dataset:

Any and all help is greatly appreciated!

In the named query, change the query type to scalar. This will prompt it to return a single value instead of a dataset.

3 Likes

Fantastic!

Did not know what a scalar query did, but that was exactly what I needed. Thanks so much for the feedback. Such an easy fix! :smile:

1 Like

scalar
/ˈskeɪlə/

Mathematics•Physics
adjective

  1. (of a quantity) having only magnitude, not direction. [A regular SQL query would have two dimensions; rows and columns.]

noun

  1. a scalar quantity.