how to set watcher based on custom field

i want to set watchers based on custopm field i'm trying this code but he don't work using script running

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.watchers.WatcherManager
String userName;
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("supp")
def someUser = issue.getCustomFieldValue(cf)
userName=someUser
def WatcherUser =ComponentAccessor.getUserManager().getUserByName(userName)
def watcherManager = ComponentAccessor.getWatcherManager()
watcherManager.startWatching(WatcherUser,issue)

2 answers

Hi,

it depends on the type of your "supp" custom field. You need to convert the result of getCustomFieldValue into a username first.

Or you can use JIRA Misc Workflow Extensions instead ;)

David

haw to do it ?
can you send me the code ?

As I said, it depends on the type of your custom field. Is it a user picker? A Single-select type field? Text field? Something else?

Then 

userName=someUser.value

That's because ScriptRunner doesn't know the type of the someUser variable. Write everything on one line instead.

the problem that when i show the result of watchermanager = user(user) 

that's why she don't work because my user is :

User 

and the result of watcherUser  is 

user (user)

Timing
Time (on server): Mon May 22 2017 17:37:55 GMT+0100 (Afr. centrale Ouest)

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

2017-05-22 18:37:55,557 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-05-22 18:37:55,557 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
at com.atlassian.jira.issue.watchers.DefaultWatcherManager.updateWatch(DefaultWatcherManager.java:167)
at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:84)
at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:75)
at com.atlassian.jira.issue.watchers.WatcherManager$startWatching.call(Unknown Source)
at Script1573.run(Script1573.groovy:13)

1 vote
Andreas Knecht Community Champion May 22, 2017

Hi Taher,

You could also do this with a simple automation rule with Automation for JIRA (full disclosure: I'm one of the authors).

add-watcher.png

So whenever the 'My User Picker' field's value changes, this rule will add the new value to the list of watchers on the issue.

You could also change the trigger to be something like 'Issue updated' and add various conditions to the rule, to only add the watcher if say the issue matched a given workflow status.

Cheers,
  Andreas

i'm using ScriptRunner for JIRA 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

120 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you