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.
Hi Pawel,
I don't think you would want to use a behaviour in this scenario. Adding a watcher to an issue doesn't seem like something that needs to be done instantaneously based on what's currently on the Edit form, which is what behaviours are good at. Further, behaviours won't work at all if someone inline edits the field from the View screen.
I think you setting up a Script Listener is what you will want. Listeners are good for what you're trying to accomplish because they don't need to look at the workflow status. For example, you can set up a listener that listens for the Issue Created and Issue Updated events. Whenever someone edits or creates an issue and changes the field value to "Yes", the listener will get triggered. The script you have above would then run to add the user as a watcher.
Does that make sense?
Regards,
Josh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Discover how Atlassian is revolutionizing service management with cutting-edge solutions for AI-powered support, HR Service Management, or DevOps connectivity.
Register here ⬇️Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.