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)
}
This was exactly what I was looking for, thanks so much for sharing the code!