Automatically adding watchers every time a specific issue type created

Elif Alverson May 6, 2016

Hello, 

How can I add specific users as watchers every time an " error " issue type created? I have checked the link below and provided scripts below does not work for me.- We use JIRA 7.1.

Thank you.

https://answers.atlassian.com/questions/95087

7 answers

3 votes
Robin Peters
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 9, 2016

Oh man, you should do some Java tutorials to learn a bit programming.

 

import com.atlassian.jira.component.ComponentAccessor
 
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
 
def issue = event.getIssue()

def user1 = userManager.getUserByName("dustan")
def user2 = userManager.getUserByName("gabby")
def user3 = userManager.getUserByName("Phillip")
def user4 = userManager.getUserByName("Gerald")
def user5 = userManager.getUserByName("elif")
def user6 = userManager.getUserByName("jon")
if (issue.getIssueType().getName() == "New Contractor")
{
  watcherManager.startWatching(user1, issue)
  watcherManager.startWatching(user2, issue)
  watcherManager.startWatching(user3, issue)
  watcherManager.startWatching(user4, issue)
  watcherManager.startWatching(user5, issue)
  watcherManager.startWatching(user6, issue)
}
2 votes
Robin Peters
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 6, 2016

Just replace "Bug" with your issuetype and "robin" with your username.

import com.atlassian.jira.component.ComponentAccessor

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

def issue = event.getIssue()
def user = userManager.getUserByName("robin")

if (issue.getIssueType().getName() == "Bug") watcherManager.startWatching(user, issue)

Regards,

Robin

Elif Alverson May 6, 2016

Robin, 

I am using script Listener to add the watchers and here is what I am getting. I am not familiar with wring scripts, so please be patient with me smile

Thank you for your understanding,

 

Robin Peters
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 6, 2016

I would insert the script to workflow.

Navigate Administration/Issues/Workflows

Edit the workflow you want to change.

Go to the "Create" transition, add a post function, chose "Script Post-Function" and there "Custom script post-function". Paste the code and publish workflow.

Robin Peters
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 6, 2016

As a listener, you have to add:

def issue = event.getIssue()

to line 5.

Elif Alverson May 6, 2016

If the project is using the default JIRA workflow - not a custom one- , will the other projects using the default Jira workflow will be impacted? Or not?

Robin Peters
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 6, 2016

If you want to use script listener, i updated my first post with the declaration of "issue".

Best,

Robin

Elif Alverson May 6, 2016

Script Listener works!!!!! Thank you so much, you made my day! smilesmilesmile

I am curious though.If I have added the transition you have mentioned above the default workflow that my project associates with  , would it impact all the other projects which have been using the default JIRA Workflow?

Robin Peters
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 6, 2016

If you edit the standard workflow, all projects using this workflow will be impacted.

So, use the Script Listener with the extended code from above. smile

Elif Alverson May 6, 2016

THANK YOU SO VERY MUCH!

Elif Alverson May 6, 2016

Robin, 

When I added multiple people on the script I am getting this message, any ideas?

image2016-5-6 12:51:5.png

Elif Alverson May 6, 2016

Robin, can you please send me the command line for adding multiple users. This is what I used , but I am getting the error message I posted above. 

 

def user = userManager.getUserByName("Amy", "Sally", "Phillip", "Jack", "elif", "john”)

Deepali Bagul March 28, 2017

Thankyou @Robin Peters the script runner works excellent on JIRA 7.2.3. But, the script failed on workflow post function (custom script).Thank you so much you made my day. 

Pritesh Jagani October 16, 2017

Hi Robin,

 

I am getting following error

Time (on server): Mon Oct 16 2017 17:37:53 GMT-0700 (Pacific Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2017-10-16 17:37:53,779 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-10-16 17:37:53,779 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: CR-2024, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: event for class: Script54
 at Script54.run(Script54.groovy:8)


Here's my script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.watchers.WatcherManager

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

def issue = event.getIssue()

def user = userManager.getUserByName("Brian66481")

if (issue.getIssueType().getName() == "RE Task") watcherManager.startWatching(user, issue)


Please help

Robin Peters
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.
November 3, 2017

Where you want to run that script? Inside of a transition?

Anat Ben Shloosh October 3, 2018

Hi,

I'm a beginner with Script Runner and your scripts are exctly what I need, but they don't work for me.

I've tried all 3 suggested scripts and got the same error: "The variable [event] is undeclared".

I'm using Jira server 7.5.3 and wish to add a watcher as a post function to a transition.

Please advise.

0 votes
Jigar D Patel May 31, 2017

1.png2.png

Here is the script I am using. Ofcourse, I am using logon name where it says firstname.lastname but it isnt working for me. 

0 votes
Elif Alverson May 6, 2016

Hello, 

Can anyone please help me to fix the line below for the script runner? 

def user = userManager.getUserByName("dustan", "gabby", "Phillip", "Gerald", "elif", "jon”) 

here is the error message I am getting:

image2016-5-6 13:1:32.png

0 votes
Denis Boisvert May 6, 2016
0 votes
Elif Alverson May 6, 2016

Steven, 

I have posted the error messages I am getting below. Any idea?

0 votes
Steven F Behnke
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 6, 2016

Why don't the scripts in that link work for you?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events