Groovy postfunction: obtain reference to epic after creating an issue from

AK February 27, 2014

Hi,

I am working on a groovy script which runs when an issue is created under Epic and copies some custom field values from the epic to the created issue.

For Ex. When issue type 'Story' is created under epic, xyz CUSTOM field value from epic should be copied to Story.

But I am not able to get hold of the reference to epic in the script ( Tried and failed to get the links using IssueLinkManager class; the method getLinkCollection() returned an empty list).

Code I used:

List links = issueLinkManager.getLinkCollection(issue,user).getAllIssues()

Could some please tell how can we get reference to the linked Epic ?


Thanks,

Aravind

2 answers

1 accepted

4 votes
Answer accepted
Chris Kent
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.
April 27, 2014

I had the same problem, but I did manage to get a link to the epic when the user story is created (using the new Agile board "create issue in epic")

The trick to getting this working is in the create screen for the User story, you MUST include the "Epic Link" field in the Screen configuration. This then makes the epic link custom field available in the post script function.

Then I used the following code to get to the epic...

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();

if (issue.getIssueTypeObject().getName() == "Story" ) {
	CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
	Issue epic = issue.getCustomFieldValue(epicLink);
	if(epic)
	{
		issue.setFixVersions(epic.getFixVersions());
                .....
	}
}

AK June 9, 2014

Thanks Chris! :) Keeping the Epic Link on create screen solved the problem!

Raju Adluru
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.
July 17, 2014

Chris

we also have similar need, when we create story from Epic (click on + sign) "create issue in epic" we want to copy some of the fields from Epic to story.

In Workflow- Create action, i am adding post funtion, where to add above script in post function?, i dont have any option to add groovy script in "Add Post functions"option, do i need to add in "Script post function" in that it is asking for "script file path"

so where should i put above script and give the location for this?

thanks and appreciate your help.

1 vote
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 27, 2014

Can you try getting the Epic issue ID from the Story's outward links?

https://developer.atlassian.com/static/javadoc/jira/5.0.5/reference/com/atlassian/jira/issue/link/IssueLinkManager.html#getOutwardLinks(java.lang.Long)

public List<IssueLink> getOutwardLinks (Long sourceIssueId)

Get links from an issue.
Parameters
sourceIssueId	Eg. from getId()
Returns
List of IssueLinks

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, 2014

I think the problem will be that the links are not available at the point that he needs it.

Tarun Sapra
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, 2014

Hi Jamie,

It could be the problem, but if the post function is at the end of all the post-functions in the create issue transition, wouldn't that qualify as a solution?

Regards

AK March 2, 2014

Hi Tarun and Jamie,

Thanks for your responses.

My script is at the end of all post functions but still the issue exists. Plus I have tried calling getOutwardLInks() also ( as per definition of getLinkCollection () gives both ingoing and outgoing issue links )

Thanks

Henning Tietgens
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 2, 2014

Links are created after the issue is created, so you don't have the link. Maybe you can get information about the Epic within the request, like in my answer here: https://answers.atlassian.com/questions/266063/groovy-script-error-in-jira-6-1-missingpropertyexeption, but I don't have tested this.

Tarun Sapra
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, 2014

Ok, this is new for me, as I always thought that link can be accessed after the "create issue" event in the post-function

Raju Adluru
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.
July 24, 2014

Tarun

can you please help me with this

we also have similar need, when we create story from Epic (click on + sign) "create issue in epic" we want to copy some of the fields from Epic to story.

In Workflow- Create action, i am adding post funtion, where to add above script in post function?, i dont have any option to add groovy script in "Add Post functions"option, do i need to add in "Script post function" in that it is asking for "script file path"

so where should i put above script and give the location for this?

thanks and appreciate your help.

Suggest an answer

Log in or Sign up to answer