how to set watcher based on custom field

taher shili May 22, 2017

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

1 vote
andreas
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 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

taher shili May 23, 2017

i'm using ScriptRunner for JIRA 

1 vote
David _old account_
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 22, 2017

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

taher shili May 22, 2017

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

David _old account_
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 22, 2017

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?

taher shili May 22, 2017

single select 

Capture.PNG

David _old account_
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 22, 2017

Then 

userName=someUser.value
taher shili May 22, 2017

don't work

taher shili May 22, 2017

Sans titre.png

David _old account_
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 22, 2017

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

taher shili May 22, 2017

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)

taher shili May 23, 2017

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)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events