Skip to content

Script Examples

Michal Szulc edited this page Feb 13, 2020 · 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
}

Save example domain instance with Grails4

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