Getting a field value of an epic from an associated task

Chris Norman December 30, 2017

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

2 votes
Answer accepted
Alexey Matveev
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.
December 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)
Chris Norman January 10, 2018

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.

Chris Norman January 11, 2018

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 Sign up to answer