Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,564
Community Members
 
Community Events
165
Community Groups

ScriptRunner Post Function vs Listener Script for Watcher

Edited

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:

listener-script.png

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")
}

 

 

2 answers

It looks like the final answer was this format:

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
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def issue = event.issue as MutableIssue

//grabing some methods
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
UserManager userManager = ComponentAccessor.getUserManager()
CustomFieldManager customField = 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 = customField.getCustomFieldObjectByName("Customer")
def cFieldValue1 = issue.getCustomFieldValue(cField1).toString()

log.warn("Show the custom field value:" + cFieldValue1)

//Adding a watcher when this condition is met (NOTE: I used contains since my custom field was a multi-select)
if (cFieldValue1.contains("Something")) {

//Alternatively, you can use what was listed above: 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")
}

Hi Lubomir.

I think your postfunction is firing for sure. That is a static compilation error and I'm sure it's not what's making that postfunction fail. Could you add a 

log.debug "I'm in the postfunction" 

That way you can check the logs to make sure that it's actually getting executed.

Cheers!

DY

Hi Daniel,

it is working now. I will put the solution here later today. Thank you for your effort so far.

cheers,

Regarding the solution, I noticed that this definition was also marked by static type checking

def cFieldValue1 = issue.getCustomFieldValue(cField1).toString()

So a added a few lines:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

and

def issue = event.issue as MutableIssue

and the code started to work as expected. So it seems that the issue declaration is not needed in post-function but in Scripted Listener is needed. I don't know why, but it did the job and after that the code was clear.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Improving the Create Issue Experience in Jira Service Management Cloud

Hello everyone!  We are very excited to announce some much needed changes to the issue create experience in JSM (the blue "create" button) at the top of the screen.  We have just starte...

178 views 7 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you