Custom field on sub-task based on parent custom field?

Eugenio Aguilar February 29, 2016

Hi,

How can I have a scripted custom field in a sub-task that is taking the value from a field in the parent. I don't mean having the value inherited through a post-function, but always having the fields in the sub-task and the parent in-sync. In my specific example, I have a "Planned Release date" in the parent and I want the sub-tasks for that story to always have this field up-to-date as well.

I tried what was suggested here: https://answers.atlassian.com/questions/284367 but that isn't working for me.

1 answer

1 accepted

2 votes
Answer accepted
Kristian Walker _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.
March 1, 2016

Hi Eugenio,

I attach an example of the code below which shows how to take the value of a Date Field from a parent issue and display it in a Date/Time type script field on the sub task.

The code for this is shown below.

Code:

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;

// Get the field and its value from the parent issue
def parentCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Start Date")
def parentCFVal = issue.parentObject.getCustomFieldValue(parentCF) as Date;

// Check if the issue is a subtask and if so return a value
if (issue.issueTypeObject.name == "Sub-task") {
// Get the value as a timestamp required by the date range picker
def startDate = new Date(parentCFVal.time);
return startDate
}

Please note if you want the returned value to be formatted you can use Java Simple Date format if you change the fields template and searcher to be free text and use code similar to the example code below.

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;
import java.text.SimpleDateFormat;

// Get the field and its value from the parent issue
def parentCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Start Date")
def parentCFVal = issue.parentObject.getCustomFieldValue(parentCF) as Date

// Check if the issue is a subtask and if so return a value
if (issue.issueTypeObject.name == "Sub-task"){
// Get the value as fomrated as Day Day Month Month Month Year Year
    return parentCFVal.format("dd/MMM/YY")
}

 

I hope this helps

Kristian

JamieA
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.
March 1, 2016

better to use issue.isSubTask() than check the issue type name really, also check that before reading the parentObject, otherwise you will get a NullPointerException.

Eugenio Aguilar March 2, 2016

This is perfect. With your suggestion (plus Jamie's suggestion) I got it working perfectly.

Thank you!

Kristian Walker _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.
March 2, 2016

Glad we were able to help Eugenio.

Kristian

Suggest an answer

Log in or Sign up to answer