Execute Permanent Query

This is a simple little script for form views. It executes the first permanent query from the list of all stored permanent queries and shows it in the form.

//This sample button script will run first permanent query in data tree
evaluate = { widget ->
def rs = widget.form.resultSet
def dataTree = rs.dataTree
def queries = dataTree.getQueries()
//Change number in get(0) to run any other permanent query, 0 will pick first DFQuery from List of all permanent queries
def DFQuery = queries.getItems().get(0)
def expression = DFQuery.getExpression()
//Query name is returned via DFItem interface, refer to Instant Jchem developer javadoc
println "Executed query name: " + DFQuery.getHandle().getInstance().getName()
def rsLock = rs.lockable.withLock('querying'){ rsEnv ->
rs.applyQuery(expression, rsEnv)