Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Create new sub-task in parent when a sub-task is resolved

Marc Scholte November 16, 2022

Hey,

I am trying to automate creating new sub-task in a parent, when a specific sub-task with the same parent is resolved. But this can occur only once.

I am trying to do this with the Listeners function within Scriptrunner, with the built-in Listener "Create a sub-task".

When the existing sub-task is resolved a comment is placed in the parent, which updates the parent. I have the listener on "Issue Updated" events.

My condition code is as follows:

issue.SubTaskObjects.every {! it.summary.contains("newsubtask")} && issue.SubTaskObjects.any ({it.summary.contains("existingsubtask") && it.status.name == "Resolved"})

Odd thing is that the Listener doesn't even trigger.

I've read through https://docs.adaptavist.com/sr4js/latest/features/workflows/validators/built-in-validators/simple-scripted-validators and https://docs.adaptavist.com/sr4js/latest/features/workflows/workflow-functions-tutorial/conditions-tutorial. But just can't get it to work.

2 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 17, 2022

Hi @Marc Scholte

For your requirement, you should use the Post-Function instead of the Listener.

First, go to your workflow, click the edit button, and select the transition to which you want to add the post function. In this example, the Done transition will be used.

post_function.png

Next, add Post-Function as shown below:-

post_function1.png

Once the Post-Function options are displayed, you need to select ScriptRunner's  Custom script post-function as shown below:-

post_function2.png

Lastly, add the sample code below to the Script Editor and publish your changes.

post_function3.png

Below is a sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager

def userManager = ComponentAccessor.userManager
def issueService = ComponentAccessor.issueService
def issueManager = ComponentAccessor.issueManager
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskManager = ComponentAccessor.subTaskManager
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)

if (issue.isSubTask()) {

final parentIssueKey = issue.parentObject.key
final issueTypeName = 'Sub-task'
final reporterKey = 'auser'
final summary = 'This is a test'
final priorityName = 'Major'

def parentIssue = issueManager.getIssueByCurrentKey(parentIssueKey)
assert parentIssue : "Could not find parent issue with key ${parentIssueKey}"

def subtaskIssueTypes = constantsManager.allIssueTypeObjects.findAll { it.subTask }
def subTaskIssueType = subtaskIssueTypes.findByName(issueTypeName)
assert subTaskIssueType : "Could not find subtask issue type with name ${issueTypeName}. Avaliable subtask issue types are ${subtaskIssueTypes*.name.join(', ')}"

def reporter = userManager.getUserByKey(reporterKey) ?: loggedInUser

def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(parentIssue)

def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(parentIssue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setReporterId(reporter.username)
setSummary(summary)
setPriorityId(priorityId)
}

def validationResult = issueService.validateSubTaskCreate(loggedInUser, parentIssue.id, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def issueResult = issueService.create(loggedInUser, validationResult)
assert issueResult.valid : issueResult.errorCollection

def subtask = issueResult.issue
subTaskManager.createSubTaskIssueLink(parentIssue, subtask, loggedInUser)
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are a few test screenshots for your reference:-

1. First, to the Sub-task that you want to transition.

test1.png

2. Transition the Sub-task to the status you require. In this example, the sub-task is transitioned to Done

test2.png

3. As expected, the Sub-task transitions to Done.

test3.png

4. Go back to the parent issue; as expected, a new sub-task is created as shown below:-

test4.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram 

Marc Scholte November 30, 2022

Awesome bro, this works like a charm!

 

This is way beyond my level of ScriptRunner though.

 

Could you also please assist me on the following, with your example it will trigger on resolving any sub-task, but I want it to trigger on specific sub-tasks, based on the summary.

I assume I would have to edit the if (issue.isSubTask()) {

 

Would you recommend adding multiple post-functions if I were to create this for more than one specific sub-task, or could I do this with an else.

Marc Scholte November 30, 2022

I've been able to realize my second question with

(issue.isSubTask() && issue.summary.contains("existingsubtask"))

and adding an else if and copy-pasting your example again.

Could I possibly shorten it?

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 1, 2022

Hi @Marc Scholte

You're on the right track; it's just that you don't need to include the else statement.

You just have to include the condition

&& issue.summary.contains("existingsubtask")

To the outermost, if condition, i.e. 

if (issue.isSubTask()) {
....
....
}

will become:-

if (issue.isSubTask() && issue.summary.contains('existingsubtask')) {
....
....

}

 I hope this helps to answer your question. :-)


Thank you and Kind regards,

Ram

0 votes
Ravi Kanth
Contributor
December 12, 2023

@Marc Scholte @Ram Kumar Aravindakshan _Adaptavist_ 

Its just awesome..

Thanks brother it works for me!!!!

Suggest an answer

Log in or Sign up to answer