Script listener

Hello,

 I have used the “create a sub-task” feature of Script Runner/Listener to create multiple sub-tasks for a specific issue type. Then I noticed the order of the subtasks I added changes when the issue type is being created. I would like to have the subtasks in an order. Is there a way to do that?

Thanks in advance.

2 answers

1 accepted

Hi Elif,

Creating subtasks via Listener is async, which means that the order is not something that you can control. On the other hand, using a post function will create the subtasks with the order you define them, from top to bottom in the transaction's post functions. If you want to go with the listener, then you can control the order by writing a custom listener where you can define the order the subtasks will be created. 

Thanos, 

To use the post functions for this occasion, I need to create a specific work flow which associated with this issue type, right?

If I understand right, you want to change the order of the subtasks depending on the issue type ? If that is the case creating a new workflow would be an overkill. I would propose to do this via a custom post function or custom listener. If I remember well we discussed a very similar issue through support some time ago and we came up with a custom script, I think you can use that script with minor modifications. 

I can make the small changes on that script except to add the project name.  This issue type is being used by several projects and I would like to be sure to have the subtasks on the correct project. Can I send you the script for you to add the project name? Thanks in advance!

If you want to paste it here, I can have a look. Please add comments in the script what exactly you want to achieve, because I don't think I understand completely. 

Please see the script below. I need to work this script on a projectname=ITSC only. Can you please add that option to it?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def parentIssue = event.issue
if (parentIssue.getIssueTypeObject().getName() != 'Employee Termination')
return

def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def summariesList = ["Subtask 0",
"Subtask 1",
"Subtask 2",
"Subtask 3",
"Subtask 4",
"Subtask 5",
"Subtask 6",
"Subtask 7",
"Subtask 8",
"Subtask 9"]
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary ->
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setPriorityId(constantManager.getPriorityObjects().find {
it.getName() == "Major"
}.id)
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
newSubTask.setReporter(parentIssue.reporter ? parentIssue.reporter : null)
// Add any other fields you want for the newly created sub task

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
log.info "Issue with summary ${newSubTask.summary} created"
}

All you have to do is to add a check in the beginning of your code, in the same way you check if the parent issue type is not Employee Termination then do nothing (return),

if (parentIssue.projectObject.name != "ITSC")
    return

If you wanted to check the key then parentIssue.projectObject.key and so on... 

I have tried to run it and for some reason it does not create any of the subtasks. Any ideas?

Are you sure that the ITSC is the name of the project and not the key ?

Here is the original script we used before. It looks like all I have to do is to remove the part says" copy the JIRA issue description next to the subtasks?  Can you help to remove only that part?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def parentIssue = event.issue
if (parentIssue.getIssueTypeObject().getName() != 'Employee Termination')
return

def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def summariesList = ["Subtask 1",
"Subtask 2",
"Subtask 3",
"Subtask 4",
"Subtask 5",
"Subtask 6",
"Subtask 7",
"Subtask 8",
"Subtask 9"]

def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary ->
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setSummary(subTaskSummary + " - " + parentIssue.getDescription())
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setPriorityId(constantManager.getPriorityObjects().find {
it.getName() == "Major"
}.id)
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
newSubTask.setReporter(parentIssue.reporter ? parentIssue.reporter : null)
// Add any other fields you want for the newly created sub task

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
log.info "Issue with summary ${newSubTask.summary} created"
}

The line 

newSubTask.setSummary(subTaskSummary + " - " + parentIssue.getDescription())

concat in the subtask summary the description of the parent issue. If you want just the summary 

newSubTask.setSummary(subTaskSummary)

Thanos, 

Thank you for all your assistance. It works perfect now!

Thanos, 

The scripts you have provided does not work anymore. I am getting error messages from 4,8,25,35 and 36th lines.  Here are the error messages i am getting.

Can you please help me with it? Thanks!

 

Here is the entire script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def parentIssue = event.issue
if (parentIssue.getIssueTypeObject().getName() != 'Job Change/Transfer Form')
return

def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def summariesList = ["aa",
"bb.)",
"cc",
"dd"]

def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary ->
MutableIssue newSubTask = issueFactory.getIssue()

newSubTask.setSummary(subTaskSummary)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setPriorityId(constantManager.getPriorityObjects().find {
it.getName() == "Major"
}.id)
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
newSubTask.setReporter(parentIssue.reporter ? parentIssue.reporter : null)
// Add any other fields you want for the newly created sub task

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
log.info "Issue with summary ${newSubTask.summary} created"
}

Elif did you upgrade to JIRA v7 ? If that is the case have a look at the guide. In your case you should replace:

Line 5: if (parentIssue.getIssueType().getName() != 'Job Change/Transfer Form')

Line 8: def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Line 25: newSubTask.setPriorityId(constantManager.getPriorities()....

 Line 35: issueManager.createIssueObject(user, newIssueParams)

Line 36: subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)

Also keep in mind that there are some times that you can safely ignore the static type checking

Yes, IT department updated all the Atlassian products last week. 

What should I replace the below line with???

newSubTask.setPriorityId(constantManager.getPriorityObjects().find {
it.getName() == "Major"
}.id)

Thanos, 

I have changed all the lines you have mentioned above except the line 25 for the script pasted below, For some reason it does not create the subtasks at all.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def parentIssue = event.issue
if (parentIssue.getIssueType().getName() != 'New Employee')
return

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summariesList = ["AA",
"BB,
"CC",
"DD",
"EE"]
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary ->
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setSummary(subTaskSummary + " - " + parentIssue.getDescription())
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setPriorityId(constantManager.getPriorityObjects().find {
it.getName() == "Major"
}.id)
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
newSubTask.setReporter(parentIssue.reporter ? parentIssue.reporter : null)
// Add any other fields you want for the newly created sub task

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
issueManager.createIssueObject(user, newIssueParams)
log.info "Issue with summary ${newSubTask.summary} created"
}

the line 25 is

newSubTask.setPriorityId(constantManager.getPriorities().find {
        it.getName() == "High"
}.id)

and try again. 

PS. I am also sure that in the original request there were two versions of the script, one for JIRAv6.* and one for JIRA v.7.*  

Thanos, the line 25 worked great thank you!!!

However i cannot login and see the original request, it does not let me to login! 

Can you please paste that script for the subtasks that you have created for V 7.0?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def parentIssue = event.issue
if (parentIssue.getIssueType().getName() != 'New Employee')
    return

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summariesList = ["summary1",
                     "summary2",
                     "summary3",
                     "summary4",
                     "summary5",
                     "summary6"]
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary -&gt;
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(subTaskSummary)
    newSubTask.setParentObject(parentIssue)
    newSubTask.setPriorityId(constantManager.getPriorities().find {
        it.getName() == "High"
    }.id)
    newSubTask.setProjectObject(parentIssue.getProjectObject())
    newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
        it.getName() == "Sub-task"
    }.id)
    // Add any other fields you want for the newly created sub task

    log.debug("New issue ${newSubTask}")
    Map&lt;String,Object&gt; newIssueParams = ["issue" : newSubTask] as Map&lt;String,Object&gt;
    issueManager.createIssueObject(user, newIssueParams)
    subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    log.info "Issue with summary ${newSubTask.summary} created"
}

Hello, 

This script above does not create any sub-tasks. Any idea why?

Thanos,

 

even though there are no error messages coming from the scrips above they dont create sub-tasks anymore. Please help!

the version we are using is v7.1.1, not sure if that is the reason.

Elif just tested it in my local instance (JIRA v7.1.1) works as expected, as a listener during an issue update. Do you get any errors in your logs ? Do you make sure that the parent issue is of type New Employee ?

Thanos, 

I have been creating it exactly the same as before, please see the attachment. It does not give me any error messges, it just does not create any subtasks at all.

could you please attach the listener configuration screen ?

Please let me know, if you have any other questions

Script works on the Sandbox as you said BUT not on the Production JIRA.

 

ok try to change the priority to something that exists (I can see that the Major is a valid priority for your instance).

newSubTask.setPriorityId(constantManager.getPriorities().find {
        it.getName() == "Major"
    }?.id)

Thanos, 

IT IS WORKING smilesmilesmilesmilesmile

You are awesome!!!!

I get the warning message of  " since V 7.0 @line 25 , column 30. But it is working smile

 

Thank you so much!!!

 

Thanos, 

I have couple of questions about the script you have provided above, would you be willing to help?

Please let me know.

 

Thank you.

sure Elif,

Just open a new thread...

Thanos, Can you please help? Thanks!

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

172 views 1 17
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