Link subtask to parent issue automatically with scriptrunner?

Connor Sims July 25, 2017

Hi,

 

Has anyone successfully created a subtask on the Create transition and subsequently linked it to its parent issue automatically in the same transition with a script?

 

I'd really appreciate if you'd share how this can be done

 

Thanks,

 

Connor

2 answers

1 vote
Danilo Lima January 3, 2019

Here´s how I did it using Script Runner:

Using a ScriptPost Function, at the end of the create transition:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import groovy.util.logging.Log4j
import org.apache.log4j.Category

log.setLevel(org.apache.log4j.Level.INFO)
MutableIssue parent = issue.getParentObject() as MutableIssue

def issueManager = ComponentAccessor.getIssueManager();
def projectIssue = issueManager.getIssueObject(parent.id);
def isProjectIssueValid = (projectIssue != null);

try{
def issueLinkManager = ComponentAccessor.getIssueLinkManager();
log.info "Link being created by " + ComponentAccessor.getJiraAuthenticationContext().getUser() + " - Parent: " + parent.id + " and child: " + issue;
issueLinkManager.createIssueLink(projectIssue.id, issue.id, 00000L, 1L, ComponentAccessor.getJiraAuthenticationContext().getUser()); // here you replace "00000" for the ID of the Issue link, keep the "L" after the ID.
} catch (Exception e) {
log.info e
}

Hope this helps.

Maik Ebert February 15, 2019

Hi Danilo,

 

your script works just fine but it always uses the inward link on the thus created subtasks (child). We use subtasks which "treat" our issues and this needs to be changed around for and specific addon to work properly and show additional informations. What do we need to change to make it work?

 

Thanks and kind regards

Maik

Like Danilo Lima likes this
Danilo Lima February 15, 2019

Hi @Maik Ebert , I'm glad to hear that it worked for you as well. To "invert" the issue link being created, in the line 21, you can just change where you see:

"projectIssue.id, issue.id" 

to

"issue.id, projectIssue.id"

This will literally "invert" the link and use the outward link.

Maik Ebert February 21, 2019

Works just fine @Danilo Lima. I added a line so it only works on specific parent tasks (cause we use our treatment subtasks on several issuetypes and i don't want all to be linked (automatically)).

Thanks again :)

Like Danilo Lima likes this
0 votes
Daniel Yelamos [Adaptavist]
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 26, 2017

Hello Connor.

I believe this can be done with the SR Create Subtask post function.

Since it is a sub task, it needs to have a parent, so if you were to add this post-function in te onCreate transaction, it would create the subtask that you are looking for.

This is the dialog that you need to fill, and it can be accessed in the add postfunction dialog in the workflow transaction editor.

Screen Shot 2017-07-26 at 10.13.43.pngIf you have more questions do not hessitate to ask.

Cheers!

Dyelamos

 

Connor Sims July 26, 2017

Dyelamos,

Thank you for the reply! I'm already able to create subtasks through this exact dialog- my question is how I can configure this dialog so that upon creation the subtask will be 'linked' to the parent issue - do you know how I could configure that?

Thanks a lot

Connor

Daniel Yelamos [Adaptavist]
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 26, 2017

Isn't that done automatically? When I create a subtask it links the parent on its own

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 26, 2017

That's a good point - a sub-task is always part of its parent and displays as such. The fact it is a sub-task means there is a link to the parent.

Creating an issue link as well is redundant (although if you looked at the code, you'd see an invisible "system" issue link is already there)

Matt Parks April 17, 2018

I'm running into something similar. There is a brief moment in time after the sub-task is created when it technically isn't linked to the parent. I know this is happening because I have a script listener that burns down the remaining estimate of the parent when the sub-task is created (this keeps the overall remaining estimate accurate up the chain so we're not double-counting, as the Remaining Estimate of the parent includes the Remaining Estimates of its children when using Portfolio).

This functionality works properly at any level above sub-tasks (e.g. when I add an issue to an Epic) when issues are created, but I have to go back into the sub-task and update it in order for the functionality to work properly at this level. This is because the script listener is triggering off of Issue Created, Issue Updated, and Work Logged On Issue events.

Since the listener works properly when the issue is updated and works properly for other issue types, it seems like the subtask and parent aren't actually linked at the time that the Issue Created event fires. I've tried adding post functions that do nothing but sleep for a second or two, but this hasn't had an impact. I've also added a sleep to the beginning of the script listener.

So, does anyone have any idea of how to get the parent/sub-task link to happen more quickly? I've also tried creating a post function that transitions the subtask back to the same status and triggers an Issue Updated event, but this also didn't have an impact.

Andrew Zibuda October 19, 2018

@Matt Parks any luck with this at all? I've just run into the same issue, and was about to try a solution similar to transitioning the subtask back to the same status...but you are saying that doesn't work. Have you tried doing that same thing in Script Listener? This is quite annoying.

Suggest an answer

Log in or Sign up to answer