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.
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.
Next, add Post-Function as shown below:-
Once the Post-Function options are displayed, you need to select ScriptRunner's Custom script post-function as shown below:-
Lastly, add the sample code below to the Script Editor and publish your changes.
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.
2. Transition the Sub-task to the status you require. In this example, the sub-task is transitioned to Done
3. As expected, the Sub-task transitions to Done.
4. Go back to the parent issue; as expected, a new sub-task is created as shown below:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Marc Scholte @Ram Kumar Aravindakshan _Adaptavist_
Its just awesome..
Thanks brother it works for me!!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.