Getting a field value of an epic from an associated task

This is what I am trying to accomplish: I have timecard charge numbers as a custom field define for all our epics. I am using arsenal dataplane to create a report for all team members that show the time they logged on tasks and subtasks. I would like to show in that report the charge code for the epic associated with the task. If you didn't follow here's another explanation of what I am trying to do: simply show the value from an epic custom field in a report that shows task information.

I think I can do this with scriptrunner custom field for a task that finds the parent epic and reads the charge number value. I would then have that information in a custom field associated with task that I can use for reports. I have created a custom field in scriptrunner for the task but can't figure out the script. Any help on writing the script would be greatly appreciated. If you don't use scriptrunner but know how to do this from Java API plugin I would love to see that code because that might be good enough for showing me how to do it. Thanks!

1 answer

1 accepted

1 vote
Alexey Matveev Community Champion Dec 31, 2017

The code would be like this

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
Issue epic = issue.getCustomFieldValue(epicLink) as Issue;
CustomField cs = customFieldManager.getCustomFieldObjectByName("customFieldName");
return epic.getCustomFieldValue(cs)

This worked beautifully! Thank you. Except. This worked for just the tasks. Is there some code that could be added such that if this is a subtask that it moves to the task and then finds the custom field.

Figured out something that works for subtasks and tasks. Again, thanks for help! See code below:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');

if (issue.isSubTask()){
issue = issue.getParentObject();
}

Issue epic = issue.getCustomFieldValue(epicLink) as Issue;
CustomField cs = customFieldManager.getCustomFieldObjectByName("Charge Code");
return epic.getCustomFieldValue(cs)

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,329 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot