Skip to content

Script Examples

Adrian A edited this page Oct 27, 2023 · 2 revisions

Print domain info.

grailsApplication.domainClasses.each {
    println "There are ${it.clazz.count()} instances of $it.clazz.simpleName"
}

Access Spring bean.

import groovy.sql.Sql
def dataSource = ctx.dataSource
def sql = Sql.newInstance(dataSource)
sql.eachRow("select * from information_schema.system_tables", {
    println it.TABLE_NAME
})

Print session variables.

session.attributeNames.each { name ->
    println name.padRight(40) + session.getAttribute(name)
}

Print request attributes.

def params = ['requestURI', 'requestURL', 'forwardURI']
params.each {
    println it + "\t" + request."$it"
}

Print all config values.

config.flatten().each { name, value ->
    println name.padRight(40) + value
}

Print all config values as a tree to the console

console.dir config

Save example domain instance with Grails4

ExampleDomainClass.withTransaction {
        ExampleDomainClass example = ExampleDomainClass.get(1)
        example.active = true
        example.save(flush:true)
    }
Clone this wiki locally