How to Add a Watcher Based on the Value of a System Field

Jose Sepulveda October 15, 2019

I'm trying to add 1 user as a watcher on a post function once a ticket is created. I'm using script runner and this is the syntax I currently have:

import com.atlassian.jira.component.ComponentAccessor 
def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def user = userManager.getUserByName("jhobson1")

if (issue.Components.equals("DART")){
watcherManager.startWatching(user, issue) }

 Essentially if the components system field equals "DART" the user should be added as a watcher. Unfortunately, it doesn't do anything.

Thanks for any help.

1 answer

0 votes
Lady Di
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 16, 2019

It’s better to do this not in the post-function when creating, but as a listener, listening to the issue creation event.
Why: Because scripts tend to grow, new ones are added, as a result, issues begin to take a longer time to create, the entire service begins to slow down.

I wrote it for MyGroovy, but I think the principle is the same for Scriptrunner.


import com.atlassian.jira.component.ComponentAccessor

//issue = getIssue('TASK-1')
issue = event.issue
def component_id = 11111 //test component



if (issue.components.find{it.id == component_id}){
startWatching(getUserByName('username'), issue)
}

def getUserByName(String userName){
ComponentAccessor.userManager.getUserByName(userName)
}

def startWatching(user, issue){
ComponentAccessor.watcherManager.startWatching(user, issue)
}

Suggest an answer

Log in or Sign up to answer