Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How add watchers in scriptrunner?

Alex
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.
November 24, 2022

Hello everyone! I have a code which add watchers in issue. 
but now I need to add a user only if the task : task type is "my task type" and it has a field "my cf select list" and the option "Yes" is selected there. Please tell me how can I do this? Thank you.

import com.atlassian.jira.component.ComponentAccessor

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

def watcher = ["username1","username2"]

watcher.each{userName->
def user=ComponentAccessor.userManager.getUserByName(userName)
watcherManager.startWatching(user, issue)
}

2 answers

0 votes
Volodymyr
Contributor
October 23, 2025

The issue is created of the specific type and has the specific value for the customfield, correct (the field is of what type (text, number, list single or multiple choice, etc.))?

I can give you old examples that I already have, and they can be rewritten using HAPI.
How to set a single user as a watcher:

import com.atlassian.jira.component.ComponentAccessor

def userName = 'user_name' // Name of the user

def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName(userName)

if (!user) {
log.warn("User ${userName} not found")
return
}

def watcherManager = ComponentAccessor.getWatcherManager()

// Apply when the issue is of "Change Request" type
if (issue.getIssueTypeId() == '42') {
watcherManager.startWatching(user, issue)
}

How to set two users as watchers:

import com.atlassian.jira.component.ComponentAccessor

def firstUserName = 'user_name' // Name of the user
def secondUserName = 'user_name' // Name of the user

def userManager = ComponentAccessor.getUserManager()
def firstUser = userManager.getUserByName(firstUserName)
def secondUser = userManager.getUserByName(secondUserName)

if (!firstUser) {
log.warn("User with name ${firstUserName} not found")
}

if (!secondUser) {
log.warn("User with name ${secondUserName} not found")
}

def watcherManager = ComponentAccessor.getWatcherManager()

// Apply when the issue is of "Bug" type
if (issue.getIssueTypeId() == '1') {
watcherManager.startWatching(firstUser, issue)
watcherManager.startWatching(secondUser, issue)
}


How to set all the people from DL into watchers (leave only one FOR loop):

import com.atlassian.jira.component.ComponentAccessor

def DL = 'WFT Business Desk Atlassian Support' // Replace the name to your DL

def groupManager = ComponentAccessor.getGroupManager()
def watcherManager = ComponentAccessor.getWatcherManager()

def membersOfDL = groupManager.getUsersInGroup(DL, false) // without inactive users

if (membersOfDL) {
// Use only one method and delete the others
// Using enhanced for-loop
for (user in membersOfDL) {
watcherManager.startWatching(user, issue)
}
// Using FOR
for (int i = 0; i < membersOfDL.size(); i++) {
watcherManager.startWatching(membersOfDL[i], issue)
}
// Using EACH
membersOfDL.each {
watcherManager.startWatching(it, issue)
}
// Using FOREACH
for (member in membersOfDL) {
watcherManager.startWatching(member, issue)
}
}
  

 

nick
Contributor
October 25, 2025

I came up with the following solution that meets my needs:

import com.atlassian.jira.component.ComponentAccessor

// get instances of the user and watcher managers
def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()

// define the list of users to be added as watchers
def
watcher = ['user1','user2','user3','user4']

// add each user from the list as a watcher to the current issue
watcher.each{
userName->
    def user=ComponentAccessor.userManager.getUserByName(userName)
    watcherManager.startWatching(user, issue)
}
Like Volodymyr likes this
0 votes
nick
Contributor
August 19, 2024

Hi, have you found a solution? I'm facing the same problem.

Suggest an answer

Log in or Sign up to answer