Forums

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

adding user to watch list via behaviours

Pawel Rozek
Contributor
November 6, 2018

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.

1 answer

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2018

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

Pawel Rozek
Contributor
November 6, 2018

Thanks, I actually didn't think of that. Great idea

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira service management, jsm webinar, ai in jsm, opsgenie integration, incident management, virtual agent, atlassian intelligence, ai-powered service desk, it operations, atlassian learning, service management webinar, team '25 recap

What’s new in Jira Service Management 🤔

Discover how Atlassian is revolutionizing service management with cutting-edge solutions for AI-powered support, HR Service Management, or DevOps connectivity.

Register here ⬇️
AUG Leaders

Atlassian Community Events