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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Service Desk

Wy are we still using email for Service Desk workflows?

...attest to the experience of an urgent approval that gets lost in the boss’s inbox and requires that special “Please Approve” email or text message. In an age where we have distributed teams...

90 views 0 2
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