Sub task transitions based on parent issue

Dennis S_
Contributor
October 16, 2018

Hi everybody,

 

i have a workflow for parent issues and a workflow for sub tasks. Now, I would like to trigger that all sub task (of the parent issue) will automatically be set to "cancelled" if the parent issue is manually transitioned to "cancelled" (the status "cancelled" already exists in both workflows).

I´ve already tried some groovy scripts to solve the problem, but obviously my groovy skills aren´t good enough.

Can somebody give me a code example to realize this automation? Where do i have to integrate the post function; in the parent issue or the sub task workflow?

 

Thank you in advance for your support.

 

Best regards

Dennis

2 answers

1 accepted

2 votes
Answer accepted
Mark Markov
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.
October 16, 2018

Hello @Dennis S_

What is your jira version? Question mentioned by @Daniel Yelamos [Adaptavist] have some deprecated methods and may not work on lastest versions.

Here is another code example, without deprecated method:


import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.workflow.TransitionOptions
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import org.apache.commons.lang3.StringUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory;

Logger log = LoggerFactory.getLogger("Subtask transition script")

//def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("RFA-467")
def
user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
Collection<Issue> subTasks = issue.getSubTaskObjects()
log.error("Found {} subtasks of <{}> issue, starting transitions", subTasks.size(), issue.getKey())
if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
IssueService issueService = ComponentAccessor.getIssueService()
CommentManager commentManager = ComponentAccessor.getCommentManager()
String comment = "Moving to *new status* status as a result of the *this transition* action being applied to the parent.";
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()

TransitionOptions transitionOptions = new TransitionOptions.Builder().skipConditions().skipPermissions().skipValidators().build()

subTasks.each {it ->
log.error("Try to transition subtask <{}>", it.getKey())
// In validateTransition method you should write transtion id, in this example 21
IssueService.TransitionValidationResult transitionValidationResult = issueService.validateTransition(user, it.getId(), 21, issueInputParameters, transitionOptions)
if (transitionValidationResult.isValid()){
IssueService.IssueResult issueResult = issueService.transition(user, transitionValidationResult)
if (!issueResult.isValid()){
String cause = StringUtils.join(issueResult.getErrorCollection().getErrorMessages(), "/")
log.error("Transition errors: {}", cause)
}
else{
log.error("transition issue <{}>, finished with result <{}>, adding comment", it.getKey(), issueResult.isValid())
commentManager.create(it, user, comment, true);
}
}
else{
String cause = StringUtils.join(transitionValidationResult.getErrorCollection().getErrorMessages(), "/")
log.error("Transition Validation errors: {}", cause)
}
}
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.
October 16, 2018

That is indeed a better way of doing it. Thanks Mark!

Dennis S_
Contributor
October 17, 2018

Hi @Mark Markov, Hi @Daniel Yelamos [Adaptavist],

 

thanks a lot for your prompt reply.

 

Looks like an appropriate solution. I have not yet had an opportunity to try it, but I will inform you as soon as possible (probably tomorrow).

 

Best regards

Dennis

Dennis S_
Contributor
October 17, 2018

The script works perfectly, @Mark Markov!

 

Thank you for your great support!

1 vote
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.
October 16, 2018

Hi Denis.

This question has already been answered here:

https://community.atlassian.com/t5/Jira-questions/Using-Post-function-script-to-Transition-Sub-Tasks/qaq-p/379790

It is very old though. Is it good enough to help you?

Cheers!

DY

Suggest an answer

Log in or Sign up to answer