Hello,
I'm fairly new to behaviours, have done some scripting, but mostly in the post functions.
Here is my scenario: if a value of a drop down field (validated) is set to specific value (yes), a user wants to be added to watch list. The value of the field can change at any point in the workflow or just by changing it from the view screen, so only if it's set, the user would be added to watch list at that point. They don't care to be removed if the value is changed. My thought was that behaviours would be the best option for this, but for some reason I am unable to get it to work.
I've added the behaviour for the project, and mapped my field (validated) and added Server Side Script (not initialiser script) as follows:
{noformat}
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import static com.atlassian.jira.issue.IssueFieldConstants.*
//check change history for field validated
def changedItem = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)?.findAll {it.field == "validated"}?.last()
//check if the value is yes
if ("${changedItem}".equals("yes")){
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def userKeyToAddAsWatcher = "Bob.myTestUser"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)
//check if watcher still exists
if (watcher) {
watcherManager.startWatching(watcher, issue)
}
else {
log.warn("User with key: ${userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}
}
{noformat}
What am I doing wrong here? Any help would be appreciated. Thank you in advance.