Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Add a list of watchers during CREATE Post-Function

Martin Hohenberg September 11, 2018

I am trying to add users as watchers during the CREATE post-function with a scriptrunner script. The following code (starting at line 236)

    def usernames = ["user1", "user2", "user3"];
    usernames.each {
        def user = ComponentAccessor.getUserManager().getUserByName("${it}")   
        if (user != null) ComponentAccessor.getWatcherManager().startWatching(user, issue)
    }

results in the following error message:

2018-09-11 17:26:13,177 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:172)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:89)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:80)
	at com.atlassian.jira.issue.watchers.WatcherManager$startWatching.call(Unknown Source)
	at Script203$_run_closure1.doCall(Script203.groovy:239)
	at Script203.run(Script203.groovy:237

I *guess* that the issue arises because the *issue* is in the process of being created and does not have all the necessary information stored at this point in time in the issue object.

Solutions from two years ago mention the deprecated method issue.getGenericValue(), but that approach seems to no longer work with WatcherManager.startWatching().

What is the preferred way of doing this?

2 answers

1 accepted

5 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Champion
September 12, 2018

Hi Martin,

 

probably you need to move your pf order. You need to move this script after "Creates the issue originally.". It should solve your issue.

Martin Hohenberg September 12, 2018

This solved the problem. Thank you :)

Fabio Racobaldo _Herzum_
Community Champion
September 12, 2018

Please, mark answer as resolved accepting my suggestion.

Ciao,

Fabio

1 vote
Fabio Racobaldo _Herzum_
Community Champion
September 12, 2018

Hi Martin,

 

try using this code :

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.watchers.WatcherManager;

import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;

String[] users = {"user1", "user2", "user3"};
UserManager userManager = ComponentAccessor.getUserManager();
WatcherManager watcherManager = ComponentAccessor.getWatcherManager();
for(String us : users){
ApplicationUser user = userManager.getUserByKey(us);
if(user!=null)
watcherManager.startWatching(user, issue);
}

 Hope this helps,

Ciao,

Fabio

Martin Hohenberg September 12, 2018

Hi Fabio,

thank you for your suggestion. Unfortunately, that does not solve my situation:

2018-09-12 09:45:18,964 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:172)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:89)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:80)
 at com.atlassian.jira.issue.watchers.WatcherManager$startWatching.call(Unknown Source)
 at Script245.run(Script245.groovy:245)

I guess the problem still lies with the issue. Is the problem related to how I define that? Line 245 is this one:

watcherManager.startWatching(user, issue);

Way up the script, there is the following line:

MutableIssue issue = issue;

 

Suggest an answer

Log in or Sign up to answer