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?