Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

443 views 4 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you