Script listener

Elif Alverson March 8, 2016

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

0 votes
Answer accepted
Thanos Batagiannis _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.
March 8, 2016

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. 

Elif Alverson March 8, 2016

Thanos, 

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

Thanos Batagiannis _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.
March 8, 2016

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. 

Elif Alverson March 8, 2016

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!

Thanos Batagiannis _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.
March 8, 2016

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. 

Elif Alverson March 8, 2016

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"
}

Thanos Batagiannis _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.
March 8, 2016

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... 

Elif Alverson March 8, 2016

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

Thanos Batagiannis _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.
March 8, 2016

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

Elif Alverson March 8, 2016

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"
}

Thanos Batagiannis _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.
March 8, 2016

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)
Elif Alverson March 8, 2016

Thanos, 

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

Elif Alverson March 21, 2016

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"
}

Thanos Batagiannis _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.
March 21, 2016

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

Elif Alverson March 21, 2016

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)

Elif Alverson March 21, 2016

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"
}

Thanos Batagiannis _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.
March 21, 2016

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.*  

Elif Alverson March 21, 2016

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?

Thanos Batagiannis _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.
March 21, 2016
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"
}
Elif Alverson March 21, 2016

Hello, 

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

Elif Alverson March 21, 2016

Thanos,

 

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

Elif Alverson March 21, 2016

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

Thanos Batagiannis _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.
March 21, 2016

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 ?

Elif Alverson March 21, 2016

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.

Thanos Batagiannis _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.
March 21, 2016

could you please attach the listener configuration screen ?

Elif Alverson March 21, 2016

Please let me know, if you have any other questions

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

 

Thanos Batagiannis _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.
March 21, 2016

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)
Elif Alverson March 21, 2016

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!!!

 

0 votes
Elif Alverson June 21, 2016

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.

Thanos Batagiannis _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.
June 21, 2016

sure Elif,

Just open a new thread...

Elif Alverson June 21, 2016

Here is the new thread ; 

https://answers.atlassian.com/questions/39148629

Thank you so much!

Elif

Elif Alverson June 21, 2016

Thanos, Can you please help? Thanks!

Suggest an answer

Log in or Sign up to answer