Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner listener to update custom date/time field when subtask reaches a status

David Mayer
Community Champion
May 1, 2019

Hello,

I have done this with a regular issue type, but for some reason the same script will not work when I define a subtask instead. my two use cases below:

 

1.)When a defect issue type reaches closed, update the defect closure date/time to the date/time the status was updated to closed. This script works:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import java.sql.Timestamp

def issue = event.issue as Issue
def issuetype = issue.getIssueType()
def statusObj = issue.getStatus()
def simpStatus = statusObj.getSimpleStatus()
def statusName = simpStatus.getName()
if (issuetype.getName() == "Testing Defect")
if (statusName == "Closed") {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Defect Closure Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}

 

2.) When a Deployment Request subtask issue type reaches Ready for Technical Verification status, update the Implementation Date/Time to the date/time the status was updated to Ready for Technical Verification. This script does not work:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import java.sql.Timestamp

def issue = event.issue as Issue
def issuetype = issue.getIssueType()
def statusObj = issue.getStatus()
def simpStatus = statusObj.getSimpleStatus()
def statusName = simpStatus.getName()
if (issuetype.getName() == "Deployment Request")
if (statusName == "Ready for Technical Verification") {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Implementation Date/Time")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}

 

 

I am guessing it has to do with defining the subtasks/issue type, but I have no failures and the script logs reference the test issue key, but doesnt update the field. Any ideas?

2 answers

1 accepted

2 votes
Answer accepted
David Mayer
Community Champion
May 2, 2019

I went about this a different way. Instead of trying to script it, I put in a post function on the transition:

 

Update issue fields 
Target issues:
Current IssueTarget fields and Source values:Target FieldType of ValueSource ValueDon't Overwrite

Implementation Date/TimeMath/Time expression{Current date and time}
1 vote
Zachary Miller May 1, 2019

Hi @David Mayer 

I tried out this script with both tasks and sub-tasks and they worked without error. My best guess would be that the names that your if statements are returning false. I would try adding some "log.error("Check")" lines before and after each if statement. Then checking the script executions to make sure that the code within the if statements have executed. 

One additional note

Rather then using ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName

I would use ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_<id>") (find the id here https://confluence.atlassian.com/jirakb/how-to-find-id-for-custom-field-s-744522503.html). This will help make your scripts be lower maintenance as that way if you update the name down the line, it will not effect your script

David Mayer
Community Champion
May 1, 2019

Thanks for the advice, but I have tried using the custom field ID before and it doesn't work for my instance for whatever reason, here is what the log says on that issue:

Time (on server): Wed May 01 2019 13:52:05 GMT-0400 (Eastern Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-05-01 13:52:05,273 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-05-01 13:52:05,274 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$8.call(Unknown Source)
 at Script229.run(Script229.groovy:18)
Zachary Miller May 1, 2019

@David Mayer 

When you write the id are you making sure to write "customfield_<id>" rather then just the id? The text "customfield_" is required. Other then that it seems that the field id you are trying to grab does not exist.

Regardless, did you check the if statements? As I said the rest of your code looked good and worked for me.

David Mayer
Community Champion
May 1, 2019

Yeah it is my if statements... but I just don't know what else to input? the issue type is right but its a sub task... that is the only thing I can think of..

 

But thanks the custom field update worked.... I am guessing that also reduces strain on the DB from having to "find" by name..

David Mayer
Community Champion
May 1, 2019

I will add "log.error("Check")" to my if statements and report back.

Suggest an answer

Log in or Sign up to answer