Automatically adds watchers based on a custom field

Fernanda de Souza Thaumaturgo
Contributor
September 19, 2019

Hi,

How can i automatically adds watchers based on a custom field in JIRA when a issue is created?


I have the ScriptRunner plugin.

Does anyone know how I can do this using scriptrunner?

If anyone knows, could you provide me a script?

3 answers

1 accepted

0 votes
Answer accepted
Mohamed Adel
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.
September 19, 2019

@Fernanda de Souza Thaumaturgo 

I recommend to use Automation for Jira Lite by adding automation rule

- when issue is created

- if JQL result passed (custom field value = {Data})

- Add watcher

Fernanda de Souza Thaumaturgo
Contributor
September 19, 2019

I already have ScriptRunner plugin, do you know if i can use this one to do this automation?

Mohamed Adel
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.
September 19, 2019

@Fernanda de Souza Thaumaturgo 

Try this code as post-function after issue created

import com.atlassian.jira.component.ComponentAccessor


//Get appropriate managers
def watcherManager = ComponentAccessor.getWatcherManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

//Get your custom field object
def customField = cfManager.getCustomFieldObject().find{it.name == "Custom Field Name"}
//Get the custom field value
def userToWatch = issue.getCustomFieldValue(customField)

//Set the user to watch the provided issue
watcherManager.startWatching(userToWatch, issue)
Fernanda de Souza Thaumaturgo
Contributor
September 20, 2019

Thanks @Mohamed Adel !

Unfortunately this code only works for the server version.

But thank you so much for your help!

Like Mohamed Adel likes this
0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 19, 2019

Hi Fernanda de Souza Thaumaturgo,

I can confirm that you will be unable to use Behaviours as responses have suggested within ScriptRunner for Jira Cloud as the cloud version of the plugin does not have the Behaviours feature like the server version does.

The reason we are unable to provide this functionality is due to the restricted functionality and API's which Atlassian provide inside JIRA Cloud, due to the fact that Atlassian only provides a Rest API in Jira Cloud and not the same Java API that the server version contains.

You can see more detailed information on the differences between ScriptRunner for JIRA Cloud and ScriptRunner for JIRA Server inside of the documentation page located here.

However, I can confirm that you can achieve this requirement inside of ScriptRunner for Jira Cloud by adding in a Run Script post function configured as the last post function, on the Create Transition of the workflow.

The script will need to get the custom field value off of the issue and if the custom field value matches the expected value then adds a user as a watcher on the issue.

I can confirm that we have lots of examples in the documentation page located here which show how to extract the values from different custom field types and that we have the example script located here which shows how you can add a user as a watcher. 

You will be able to take these scripts and will be able to use these as a reference guide to help create the script that you require.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

0 votes
Joe Pitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 19, 2019

You can't out of the box. You will need a plugin. I believe the Behaviors plugin will allow you to do it 

Fernanda de Souza Thaumaturgo
Contributor
September 19, 2019

I already have ScriptRunner plugin, do you know if i can use this one to do this automation?

Suggest an answer

Log in or Sign up to answer