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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

97 views 0 5
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