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

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

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

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

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 Champion Feb 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

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

Tarun Sapra Community Champion Mar 01, 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

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 Community Champion Mar 02, 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 Champion Mar 02, 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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Sarah Schuster
Posted Mar 28, 2018 in Jira Software

Can a company’s culture make or break agile adoption?

Can a new-to-agile team survive and thrive in a non-agile culture? If so, what advice would you give to those trying to be agile in a non-agile culture? What's the key(s) to success? Share your thoug...

12,068 views 15 13
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