ScriptRunner Listener add User Picker field name as Watcher

Gordon Rutherford January 27, 2022

Hi

Just wanted to share the code that @Benz Kek _Adaptavist_ helped me with for creating a listener that adds a user who is added to a jira issue in a user picker, as a watcher of the issue. As always the support from the Adaptavist team is fantastic - thanks again Benz.

We have a number of different people working on various Jira issues who are added under different userpicker custom fields by their managers, previously the manager would have to remember to manually add them as watchers.

Examples are 'photographer' or 'stylist'

The code below is the script inserted in a SR Listener, fired from (in my case) the' issue updated' event

It will only add the user as a watcher if have not already been added

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def watcherManager = ComponentAccessor.watcherManager
def userManager = ComponentAccessor.userManager

def issue = event.issue
def singleUserPickerFieldName = "User Picker Custom Field"
def singleUserPicker = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(singleUserPickerFieldName).first()
def user = (ApplicationUser) issue.getCustomFieldValue(singleUserPicker)

def currentWatcherUsernames = watcherManager.getCurrentWatcherUsernames(issue)
if (!(user.username in currentWatcherUsernames)) {
watcherManager.startWatching(user, issue)
}

 

2 comments

Comment

Log in or Sign up to comment
Tim Perrault
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.
January 27, 2022

Nice. I can see this being very useful!

Sue Tennant March 2, 2023

This was exactly what I was looking for, thanks so much for sharing the code!

TAGS
AUG Leaders

Atlassian Community Events