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

ScriptRunner for Jira Cloud: Add watcher in ScriptRunner Post-Function

What is exact script to add single user as watcher?

3 answers

1 accepted

1 vote
Answer accepted

Thanks, but it was too late.

My working code:

.header('Content-Type', 'application/json')

Hi, Randy. Thanks.



  1. What should be in POST body and how to pass it to method call?
  2. How to get issue key when creating issue (not modifying one)?

1. you only need to include post body contents if the watcher you're setting is different from the executing user.  

2. This is a different issue from your original question but when you submit a create request the call will return the issuekey.  You cant run post functions on create transition reliably unfortunately, use a listener instead and there will be an issue binding available.

Hi VVeider,

Thank you for your question. 

I can confirm that as Randy has said that to add a watcher with ScriptRunner for Jira Cloud that you will need to make a rest API call to the watchers API. 

I can confirm that we have an example code snippet located here which can be run in the Script Console and shows how you can set the watcher on an Issue and that you will be able to take this example and modify it to create the post function that you require.

I can confirm that inside the post function that there is a variable called issue which comes in the binding of the script and you can get the issue key to pass into the rest call by calling issue.key.

Finally, I can advise that if you are creating your post function on the Create transition that the post function should be ordered as the last post function in the list of post functions, so that the issue is created before the post function code is executed. 

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.



Kristian thanks for this post

Since October I was trying to solve this problem.

This is my code to automatically add watchers from a certain JIRA group to a newly created Incident issue:

def issueKey = issue.key
def group = get('/rest/api/3/group/member')
.header('Content-Type', 'application/json')
.queryString("groupname":"IT Department")
def result = post('/rest/api/2/issue/' + issueKey + '/watchers')
.header('Content-Type', 'application/json')

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