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

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

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

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{ == component_id}){
startWatching(getUserByName('username'), issue)

def getUserByName(String userName){

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

Suggest an answer

Log in or Sign up to answer

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