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

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

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

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.

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

Thank you!

Glad we were able to help Eugenio.

Kristian

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,402 views 15 19
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you