It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Adding a few watchers through scriptrunner if email contain "Job-Failed" Edited

i want to add a new post-function on workflow to achieve auto adding watchers function for service desk project.

like:   when email subject contain "Job-Failed", then ticket auto add a few watchers

          when email subject does not contain" Job-Failed", then create ticket as normal

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

// i think "Summary" is the subject field of email
def cf = cfManager.getCustomFieldObjectByName("Summary")

if (issue.getCustomFieldValue(cf).toString().equals("Job-Failed")) {

def watchUsers = { usernames ->
usernames.each {
def users = userManager.getUserByKey(it.toString())
watcherManager.startWatching(users, issue)
def users = ["seed", "ops", "ewss", "wwd", "ssee"]



 This is my code, some errors came out from:

if (issue.getCustomFieldValue(cf).toString().equals("Job-Failed"))

 Can anyone give me some suggestions? i am not good at groovy. 

2 answers

Firstly, `Summary` is not a custom field - you can just use the `getSummary()` method on the issue - see here 


Secondly, your method for adding watchers will not work, you need to pass it an ApplicationUser, currently you are passing it an array of strings. The logic of this method is also incorrect.


You should loop over your array of users and return an ApplicationUser for each of the users by using the getUserByName() method from the UserManager library - see here 


you can then pass this through to the startWatching() method as it expects an ApplicationUser - see here 



I hope this helps!



Can u provide me suggested code? 

sorry, i am not a programmer, just a jira administrator.  

Hi Tim,

Thank you for your question.

I can confirm that the reason that your code above will not work inside of ScriptRunner for Jira Cloud is due to the fact the code you have provided is for ScriptRunner for Jira Server and this will not work as Atlassian only provides a rest API in Jira Cloud and does not provide a Java API in the cloud-like they do in Jira Server.

You can see more detailed information on the differences between the cloud and server versions inside of our documentation page located here.

We would recommend reviewing the documentation for ScriptRunner for Jira Cloud which is located here along with the Jira Cloud Rest API Documentation in order to see how the REST API's work in Jira cloud.

I can confirm that as for setting watchers with ScriptRunner for Jira Cloud that we have an example script located here which can be run on the Script Console to set a specified list of users defined by their accountId as watchers.

You will be able to take this script and use it as a reference guide to help create the post function 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.



Suggest an answer

Log in or Sign up to answer

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you