Forums

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

Auto watchers on create transition

manikanta ch October 7, 2020

Hello All,

I have added below script in script runner postfunction on create transition to automatically add user to the Watcher field on create transition. But below script is adding only single user to the watcher field, How do i add multiple users to watchers field?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue


def userName = "Atanu_Mandal" // name of the user
def userManager = ComponentAccessor.getUserManager()
MutableIssue issues = issue as MutableIssue
def user = userManager.getUserByName(userName)

def watcherManager = ComponentAccessor.getWatcherManager()

watcherManager.startWatching(user, issue)

Thanks in advance,

CH

2 answers

1 accepted

0 votes
Answer accepted
Raziman Dom - Ricksoft
Community Champion
October 8, 2020

If you have a collection of usernames, you can use for loop to add those users to the watchers. This code is untested but should give you a rough idea.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue


List userNameList = ["Atanu_Mandal", "user1", "user2"]

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

MutableIssue issues = issue as MutableIssue

for (def userName: userNameList) {
def user = userManager.getUserByName(userName)
watcherManager.startWatching(user, issue)

}

manikanta ch October 8, 2020

Thanks @Raziman Dom - Ricksoft . It worked

0 votes
Will C
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.
October 7, 2020

Your script is only getting one user, what other users do you want to add as a watcher and how are you fetching them? are they going to be hardcoded in?

Suggest an answer

Log in or Sign up to answer