Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×We are using Groovy script as a post function during the create transition and it errors as below
package com.onresolve.jira.groovy.canned.workflow.postfunctions
import com.atlassian.jira.ComponentManager
def componentManager = ComponentManager.getInstance()
def watcherManager = componentManager.getWatcherManager()
def userManager = componentManager.getUserUtil()
def watchUsers = {usernames ->
usernames.each {
def user = userManager.getUser(it)
watcherManager.startWatching(user, issue.getGenericValue())
}
}
//Users can be added and removed from this list below. Make sure the names are in quotes and are comma separated.
// def users =
Error as below
Can anyone assist on this? We have JIRA v7.1.10.
Complete code
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.watchers.WatcherManager import com.atlassian.jira.user.util.UserManager WatcherManager watcherManager = ComponentAccessor.getWatcherManager() UserManager userManager = ComponentAccessor.getUserManager() def watchUsers = {usernames -> usernames.each { watcherManager.startWatching(userManager.getUserByName((String)it), issue) } } def users = ["lli", "skaravadi"] watchUsers(users)
Hi @Tarun Sapra,
Thank you for the code, this worked perfectly for adding users as default watchers. However I am struggling while trying to add group as default watcher.
Could you please let me know if it is possible to add group as default watcher?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.util.GroupManager
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.group.search.GroupPickerSearchServiceImpl
import java.lang.String
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
GroupManager groupManager = ComponentAccessor.getGroupManager()
def watchUsers = {groupnames ->;
groupnames.each {
watcherManager.startWatching(groupManager.getGroupByName((String)it), issue)
}
}
def grp = ["GrpTGJiraConfluenceSupport"]
watchUsers(grp)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Shivani Chauhan
Can you please create a separate question for this, as replying on an old thread which already has an solved question would create confusion for the users.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Tarun Sapra,
Thank you for the response. I have posted a new question. Could you kindly help us on that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you tried updated version of my code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, i did try and i am getting the below error
image2017-2-1 14:24:10.png
image2017-2-1 14:24:38.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
As I have mentioned in my answer that componentManager is not used anymore and instead ComponentAccessor.
Instead of "componentManager.getUserUtil()" it should be
ComponentAccessor.getUserUtil()
@vasiliy There is a typo in your answer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Getting the below error
image2017-2-1 14:41:6.png
image2017-2-1 14:41:34.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
you need to Cast it to string - like (String)it
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
please see my complete code answer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Getting the below error after removing ComponentAccessor.getInstance()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Below is the complete script
package com.onresolve.jira.groovy.canned.workflow.postfunctions
import com.atlassian.jira.component.ComponentAccessor
def componentManager = ComponentAccessor.getInstance()
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserUtil()
def watchUsers = {usernames ->
usernames.each {
def user = userManager.getUser(it)
watcherManager.startWatching(user, issue.getGenericValue())
}
}
//Users can be added and removed from this list below. Make sure the names are in quotes and are comma separated.
def users = ["lli", "skaravadi"]
watchUsers(users)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Nice!!
Please accept / upvote the answers provided by users who helped you come up with the answer. Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still facing issues.Below are the error messages.Could you advice?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
remove the line - ComponentAccessor.getInstance() it's not required. As all the getter methods in the ComponentAccessor class are static thus call them directly like
ComponentAccessor.getWatcherManager() ComponentAccessor.getUserManager()
etc
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here is refactored version. Try this one. It is not clear for me where watchUsers variable is initialised, but it should work better:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.watchers.WatcherManager import com.atlassian.jira.user.util.UserManager WatcherManager watcherManager = ComponentAccessor.getWatcherManager() UserManager userManager = componentManager.getUserUtil() def watchUsers = {usernames -> usernames.each { watcherManager.startWatching(userManager.getUserByName(it), issue) } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
"UserManager userManager = componentManager.getUserUtil()"
UserUtil can't be assigned to instance of UserManager and componentManger needs to be replaced by ComponentAccessor.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here is more one:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.watchers.WatcherManager import com.atlassian.jira.user.util.UserManager WatcherManager watcherManager = ComponentAccessor.getWatcherManager() UserManager userManager = ComponentAccessor.getUserUtil() def watchUsers = {usernames -> usernames.each { watcherManager.startWatching(userManager.getUserByName(it), issue) } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This again won't work as this statement is wrong
UserManager userManager = ComponentAccessor.getUserUtil()
I have the provided the correct answer and has been accepted "Complete code" please see that for correct code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The error is coming because your using the wrong class, instead of ComponentManager please use ComponentAccessor
https://developer.atlassian.com/static/javadoc/jira/latest/reference/com/atlassian/jira/ComponentManager.html - ComponentManager doesn't have the methods you need in latest JIRA API Docs
While ComponentAccessor has all the methods which you are using like (getWatcherManager()) - https://developer.atlassian.com/static/javadoc/jira/latest/reference/com/atlassian/jira/component/ComponentAccessor.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.