Scriptrunner External Resources. Retrieve, update database

Kazimierz Tyczynski March 2, 2021

Hello All,

are the tasks below possible in Scriptrunner? Unfortunately, the documentation is very poor it comes to adding to the external database. I am asking for help, unfortunately I am new to Scriptrunner:

  1.  Read/ write SQL in an external database (or Read/ write CSV or Excel)
  2. Conditionnal display of a field
  3. Fill in custom field from external database
  4. Email notifications

...

Thank you in advance for your help and advices

--

Kazimierz

2 answers

0 votes
Kazimierz Tyczynski March 4, 2021

Thank you very much for your answer. I would like to clarify the statement "write SQL in an external database" is it possible to fill in a blank database? e.g. with 

db.executeInsert("INSERT INTO TABLE MyTable VALUES (?, ?, ?)", ['some', 123, myVar])

?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2021

Sure it would look like this:

def keys = DatabaseUtil.withSql('your_scriptrunner_resource_name'){sql->
sql.executeInsert('INSERT INTO TABLE MyTable VALUES (?, ?, ?)', ['some', 123, myVar])
}

log.info "${keys.size()} row(s) were inserted. List of keys: $keys"
Like Jon Kocen likes this
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 3, 2021

The DatabaseUtil is only a shortcut to obtaining a groovy.sql.Sql object.

So it's not really their job to document that class.

You'll find that documentation in pages such as: https://groovy-lang.org/databases.html and https://docs.groovy-lang.org/latest/html/api/groovy/sql/Sql.html

But short pointers:

  • To read data, you will probably want to use sql.rows() 
  • To update data, you will use sql.execute()
  • This won't help you with Excel or CSV, for that, you will need to find other methods capable of parsing such files. It is not in scope of scriptunner. But I've had some luck myself using com.opencsv.CSVReader for CSV (never tried to work with excel)
  • Conditionally display a field. Not sure what you want here or how that related to scriprunner resources. To hide fields, you'll generally want to use scriptrunner behaviours.
  • Email notifications are also not relevant in the context of db resources. Notifications are usually the result of workflow postfunctions, scripted listener or scripted jobs.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events