clear assignee postfunction script runner (using clone and move postfuction)

Kaushal Patel July 12, 2018
Version of JIRA
7.4.2
What version of ScriptRunner are you using?
5.4.12
Description

how i am trying to use a post function to clone a move the ticket to a project . that is working fine.

then i want to added myself as watcher which i found on your site

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()

doAfterCreate = {
def userKeyToAddAsWatcher = "anuser"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)

if (watcher)

{ watcherManager.startWatching(watcher, issue) }

else {
log.warn("User with key: $

{userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}
}

but i also want to set the assignee to unassigned on the clone ticket.

i have tried add currentIssue.setAssigneeId(null)
so it is like 
import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()

doAfterCreate = {
def userKeyToAddAsWatcher = "anuser"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)

if (watcher) { watcherManager.startWatching(watcher, issue) currentIssue.setAssigneeId(null) }
else {
log.warn("User with key: ${userKeyToAddAsWatcher}

does not exist therefore would not be added as a watcher")
}
}

but that hasn't worked, any help would be great.

one other thing would I be able to add the watcher to clone ticket by using the assignee on the original ticket.

thanks

 

Screen Shot 2018-07-12 at 12.00.49.png

1 answer

1 accepted

1 vote
Answer accepted
Aidan Derossett _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.
July 12, 2018

Hiya @Kaushal Patel!

I've played around with your code and have come up with a solution that is currently working on my end.

I believe you were having trouble setting the assignee to "null" simply because the issue wasn't being updated with the database after you had set the value. Because of this, none of the changes that you made were being reflected on the new issue itself. To fix this, I've added a line of code that will update the issue and bring it inline with the DB. 

As for getting the assignee of the source issue, you can do that by accessing the source issue object, which is available as a provided variable in the post-function. I've added two lines that should get the assignee from the original issue and set the user as a watcher for the new issue:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.issueManager

doAfterCreate = {
def userKeyToAddAsWatcher = "anuser"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)
def sourceAssignee = sourceIssue.assignee //Get the assignee of the source issue being cloned

if(watcher)
{
watcherManager.startWatching(watcher, issue)
watcherManager.startWatching(sourceAssignee, issue) //Add the source issue's assignee as a watcher

issue.setAssigneeId(null)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false) //Update the new issue
}
else
{
log.warn("User with key: ${userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}

I've added some comments to the above code that will help explain which lines I added in and what they do. Try out this code and let me know if it works for you! :D

If something explodes, let me know and I'll see how I can help. :)

Best,
Aidan

Kaushal Patel July 12, 2018

hi 

it didn't work with the above 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.issueManager

doAfterCreate = {
def userKeyToAddAsWatcher = "anuser"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)
def sourceAssignee = sourceIssue.assignee //Get the assignee of the source issue being cloned

if(watcher)
{
watcherManager.startWatching(watcher, issue)
watcherManager.startWatching(sourceAssignee, issue) //Add the source issue's assignee as a watcher

issue.setAssigneeId(null)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false) //Update the new issue
}
else
{
log.warn("User with key: ${userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}

but it did work when i changed if(watcher) to if(sourceAssignee) i think it must of been a type thanks for all the help . to confirm the code that worked for me was 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.issueManager

doAfterCreate = {
def userKeyToAddAsWatcher = "anuser"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)
def sourceAssignee = sourceIssue.assignee //Get the assignee of the source issue being cloned

if(sourceAssignee)
{
watcherManager.startWatching(watcher, issue)
watcherManager.startWatching(sourceAssignee, issue) //Add the source issue's assignee as a watcher

issue.setAssigneeId(null)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false) //Update the new issue
}
else
{
log.warn("User with key: ${userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}
Aidan Derossett _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.
July 13, 2018

Oh shoot, good catch!

That "if(watcher)" condition will only work if the user with the user-key "anuser" exists in your instance. Otherwise the condition will fail and won't update the watchers or assignee field. For instance, when I was testing locally, I changed the String "anuser" to "admin" instead, because admin is a user that exists in my instance. Thanks for letting me know what solution actually worked for you. 

To ensure that other people can also get some guidance from this answer, would you mind accepting either my response or a response of your own with the solution? That way the question will be marked as answered and will have a better chance of catching the attention of others looking for help. :D

Best,
Aidan

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events