Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,514
Community Members
 
Community Events
168
Community Groups

update customfield in workflow postfunction with scriptrunner

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

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

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. 

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?

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

Atlassian Community Events