Forums

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

How to add automatically watchers based on custom field

Karim Belhadj
Contributor
September 27, 2019

Hello team i would like to add watchers from 2 custom fields (user picker) .

I need your help please .

 

I use this code but it doesn't work.

 

import com.atlassian.jira.component.ComponentAccessor

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

def user = userManager.getUserObject("Michellee") 
if (user) watcherManager.startWatching(user, issue.genericValue)

 

Regards

1 answer

1 vote
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 30, 2019

Hello Karim,

Thank you for reaching out.

Using a Scriptrunner post function, I believe the Groovy Script below should work for you. P.S: Assuming your custom field ID is "12345" and its type is a text (single line):

import com.atlassian.jira.component.ComponentAccessor

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

def cf = cfManager.getCustomFieldObject((Long) 12345)
def user = userManager.getUserByName("username")

if (issue.getCustomFiledValue(cf).equals("value")){
watcherManager.startWatching(user, issue)
}

Also, you can check the three threads below for other solutions using Groovy Script:

Adding a watcher through scriptrunner if a custom field is a certain value

How to set watcher based on custom field with Scriptrunner

I'd like to add watchers when a custom field value is true

Let me know if this information helps.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events