Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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

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
Community showcase
Asked in Jira Service Desk

Calling all Insight users, we need your help!

Hello Insight users,  As part of our (Mindville's) acquisition by Atlassian, our training team is looking to build some new Insight training materials. It would really helpful if you can ...

135 views 1 0
View question

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you