Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Scriptrunner listener copy field value from subtask to parent

Mike Rathwell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 20, 2018

I have a use case where I am trying to get the first start and last end date to copy up from all subtasks in an issue to the parent. So far I am working in the Scriptrunner console to get it close before I turn it loose in a listener. I am able to capture all the information I need to set the value but it throws a "Cannot find matching method" error on the line that ostensibly should set the field value. What I have so far is based on a single subtask (for dev and debug) which DOES find the dates I want and the parent issue

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import java.sql.Timestamp;
import java.util.Date.*

def issueManager = ComponentAccessor.getIssueManager();
def startDateField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11014");
def endDateField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11015");
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Issue sourceIssue = issueManager.getIssueObject("ITPROJECTS-295");
Issue parentIssue = sourceIssue.getParentObject();

def parentStartDate = parentIssue.getCustomFieldValue(startDateField)
def parentEndDate = parentIssue.getCustomFieldValue(endDateField)

Date startResult = null
Date endResult = null
def startResultTimeStamp
def endResultTimeStamp
Collection subTasks = parentIssue.getSubTaskObjects();
for(subtask in subTasks) {
def subTaskStartDate = subtask.getCustomFieldValue(startDateField) as Date
def subTaskEndDate = subtask.getCustomFieldValue(endDateField) as Date
if (startResult == null || (subTaskStartDate < startResult)){
startResult = subTaskStartDate
}
if (endResult == null || (subTaskEndDate > endResult)){
endResult = subTaskEndDate
}
}
if (parentStartDate != startResult && parentEndDate != endResult)
// return "${startResult} - ${endResult} for ${parentIssue} having Start ${parentStartDate} and End ${parentEndDate}"
parentIssue.setCustomFieldValue(parentStartDate, startResultTimeStamp)

This returns  (with the return line uncommented)  "2018-10-19 00:00:00.0 - 2018-12-05 00:00:00.0 for ITPROJECTS-518 having Start null and End null" which is what I expect. However the last line in the script is the one that is throwing the "Cannot find matching method" error. Anyone with ideas?

3 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Gabriel Tessarini
Contributor
November 21, 2018

Hello Mike.
If you want to see, there some just done scripts in Groovy for this your requirement specifically to work with custom fields in tasks (like time spent or whathever) and sub-tasks and native fields in Jira.

You can look at: https://github.com/GTessarini/JiraAutomations

I hope help you!

Regards

 

Gabriel Tessarini

Mike Rathwell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2018 edited

Thanks @Gabriel Tessarini, I'll have a look. That wasn't some of what popped up when I was grepping Google for guidance (and is one of the next iterations that I'll be mucking about with)

Mike Rathwell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2018

@Gabriel Tessarini, I accepted YOUR answer as well as that addresses my next iteration on this festival. Much appreciated

Like Gabriel Tessarini likes this
0 votes
Answer accepted
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 20, 2018

Hello @Mike Rathwell

The thing is, that method getParentObject() returns Issue object, that is immutable class and doesnt have set methods. So you need to get MutableIssue first, like this. And do not forget to update issue to store changes.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import java.sql.Timestamp;
import java.util.Date.*

def issueManager = ComponentAccessor.getIssueManager();
def startDateField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11014");
def endDateField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11015");
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Issue sourceIssue = issueManager.getIssueObject("ITPROJECTS-295");
MutableIssue parentIssue = issueManager.getIssueObject(sourceIssue.getParentObject().getKey())

def parentStartDate = parentIssue.getCustomFieldValue(startDateField)
def parentEndDate = parentIssue.getCustomFieldValue(endDateField)

Date startResult = null
Date endResult = null
def startResultTimeStamp
def endResultTimeStamp
Collection subTasks = parentIssue.getSubTaskObjects();
for(subtask in subTasks) {
def subTaskStartDate = subtask.getCustomFieldValue(startDateField) as Date
def subTaskEndDate = subtask.getCustomFieldValue(endDateField) as Date
if (startResult == null || (subTaskStartDate < startResult)){
startResult = subTaskStartDate
}
if (endResult == null || (subTaskEndDate > endResult)){
endResult = subTaskEndDate
}
}
if (parentStartDate != startResult && parentEndDate != endResult)
// return "${startResult} - ${endResult} for ${parentIssue} having Start ${parentStartDate} and End ${parentEndDate}"
parentIssue.setCustomFieldValue(parentStartDate, startResultTimeStamp)
issueManager.updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
Mike Rathwell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2018

Thanks, @Mark Markov. I'll give that a shot. Please ignore some of the other detritus in that script as it is full of debuggish/try this crap.

Question - should i push a timestamp or a date into the CF? I can do either; use date for the compares (lazy, i know but works) and timestamp to populate the field.

Mike Rathwell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2018

@Mark Markov That was exactly the ticket. I tried Mutable BUT thinking back, I declared it wrong. You solved this issue for me

0 votes
Tanu
Contributor
January 16, 2020

Mark Markov  can you please help me in writing a script where , when subtask custom field gets updated same should reflect in parent task aslo.

Looking forward to your help

Gabriel Tessarini
Contributor
January 16, 2020

Hello @[deleted] , I don't know if you already have help in your project, but in my code repository for this Jira Automations, I have some scripts that may be applied to your need. Check it out:

https://github.com/GTessarini/JiraAutomations

TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events