Hello,
I have created a post-function script for adding a watcher to the issue when a custom field is equal to a value. But then I realized that the custom field can be changed so I decided to create another script as a script listener custom script, where I want to detect issue updated event and update watcher accordingly.
The problem is that the function for updating watcher is not working as in post-function script. Could you help me, what to change?
The error in script listener is like this:

and the script is at this time the same for post-function and listener (just copied it from post-function to listener script):
log.warn("Listener is fired");
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.CustomFieldManager
//grabing some methods
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
UserManager userManager = ComponentAccessor.getUserManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//Get the custom field value. .toString() is a must for the commentManager.create function, othervise it is throwing a validation error.
def cField1 = customFieldManager.getCustomFieldObjectByName("Customer")
def cFieldValue1 = issue.getCustomFieldValue(cField1).toString()
log.warn("Show the custom field value:" + cFieldValue1)
//Adding a watcher when this condition is met
if (cFieldValue1 == "Something") {
def users = ["UserName"]
def watchUsers = {usernames ->;
usernames.each {
watcherManager.startWatching(userManager.getUserByName((String)it), issue)
}
}
watchUsers(users)
log.warn("true")
} else {
log.warn("false")
}