Forums

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

Auto watchers to the tickets based on custom field value (multi selection)

Lakshmi CH
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.
May 31, 2023

Hi Team,

We have a requirement to add watchers to the ticket based on a custom field value.


Add Watchers to the ticket automatically.

Add User1 and User2 if the CustomField is Value1

Add User3 and User4 if the CustomField  is Value2

If both Values are selected add all 4  as watchers  (User1, User2, User3, and User4)

3 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 31, 2023

Hi @Lakshmi S

For your requirement, it would be best to use ScriptRunner' Listener.

Below is a working sample code for your reference:-

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

def issue = event.issue

def userPicker1 = issue.getCustomFieldValue('Multi User Picker 1') as List<ApplicationUser>
def userPicker2 = issue.getCustomFieldValue('Multi User Picker 2') as List<ApplicationUser>

def watcherManager = ComponentAccessor.watcherManager

def english = new Locale('en')
def currentwatchers = watcherManager.getWatchers(issue, english) as List<ApplicationUser>

if (userPicker1 || userPicker2) {
currentwatchers.each {
watcherManager.removeAllWatchesForUser(it)
}

currentwatchers = userPicker1 + userPicker2

currentwatchers.each {
watcherManager.startWatching(it, issue)
}
}

Please note that the working sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Listener configuration:-

listener_config.png

If you observe the Listener configuration above, the Issue Created and Issue Updated events have been added.

Hence, when the issue is created, the Watcher list will be updated if any user is selected from either of the Multi-User pickers. 

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

0 votes
David Fischer
Community Champion
May 31, 2023

Hi @Lakshmi CH 

You can use a Set Field Value post function, select the "Watchers" field, and use this script as the value:

def watchers = ( issue.get("Watchers") ?: [] )*.username
if ("Value1" in issue.get("Custom field")*.value)
watchers += ["user1","user2"]
if ("Value2" in issue.get("Custom field")*.value)
watchers += ["user3","user4"]
return watchers

Suggest an answer

Log in or Sign up to answer