update customfield in workflow postfunction with scriptrunner

Rick Rokosch February 4, 2021

Hi there,

I´m at the beginning of my jira experiences with scriptrunner. I have some rudimentary scripting know how and try to figure out how script runner works.

My problem: I try to update a custom field at a postfunction in my workflow. The inline script goes green but nothing happens to my custom field when I change the state of my issue. Unfortunatly I dont get any output at the log of the postfunction.

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp

// get customfield
def cFManager = ComponentAccessor.getCustomFieldManager()
def cField = cFManager.getCustomFieldObjects(issue).find{it.name == "TestField"}
// get date
def today = new java.sql.Timestamp(new Date().getTime())
// set value
if(issue.getCustomFieldValue(cField) == ""){
issue.setCustomFieldValue(cField, today)
}
//create log
log.debug today
log.debug cField

 Think I have some 

2 answers

1 accepted

1 vote
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 4, 2021

The script looks broadly right to me, but it assumes a couple of things and does not do any trapping for if those assumptions are wrong. 

It's usually fine to code that way, but I'm explaining it this way because it helps understand the what your script is doing.  If you don't code for an assumption being wrong, then the script will fall over and stop.  I think that's what you are seeing - the script is failing, stops and hence never gets to the log lines you have.

The assumption here is that you have a field named  "Testfield" and that it exists for the current issue.  There's also the problem that an empty customfield does not contain "", it doesn't exist.   I would change the script to check for those and try it again:

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp

// get customfield
def cFManager = ComponentAccessor.getCustomFieldManager()
def cField = cFManager.getCustomFieldObjects(issue).find{it.name == "TestField"}

assert cField: "Could not find field TestField"

// get date
def today = new java.sql.Timestamp(new Date().getTime())

// set value
if( ! issue.getCustomFieldValue(cField) ){
issue.setCustomFieldValue(cField, today)
}
//create log
log.debug today
log.debug cField
Rick Rokosch February 4, 2021

See below.. script is working now but still no log. Maybe I have to use log.debug another way. At log tab I see the hint:

image.png

But also when I try to use "log.info cField" nothing happens in my log. 

0 votes
Rick Rokosch February 4, 2021

Damn I figured it out with Script Console feature. The value is not "" it is null so he was not in my if clause.

So I just had to change one line:


if(issue.getCustomFieldValue(cField) == null){

Maybe someone can help me how the log function works that I have a usefull output in log tab?

Rick Rokosch February 4, 2021

Also figured it out final code and log function works fine:

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import org.apache.log4j.Logger
import org.apache.log4j.Level

//create log
log.setLevel(Level.INFO)
def output = ""

// get customfield
def cFManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def cField = cFManager.getCustomFieldObjects(issue).find{it.name == "Start Date"}

// get date
def date= new java.sql.Timestamp(new Date().getTime())

// set value
if(issue.getCustomFieldValue(cField) == null){
issue.setCustomFieldValue(cField, date)
output += "new date set"
}
output += " - done"
log.info(output)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events