Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

ScriptRunner Listener add User Picker field name as Watcher

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

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.
Jan 27, 2022

Nice. I can see this being very useful!

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

Comment

Log in or Sign up to comment