Forums

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

Adding a watcher through scriptrunner if a custom field is a certain value

Ovidiu Vasilescu
Contributor
July 3, 2018

I'm trying this:

 

import com.atlassian.jira.component.ComponentAccessor

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()

def issue = event.getIssue()
def user = userManager.getUserByName("username")

if (cfValues['FIELD']*.value.contains("VALUE")) {

watcherManager.startWatching(user, issue)

}

 

And I'm getting this error:

groovy.lang.MissingPropertyException: No such property: event for class: Script51
	at Script51.run(Script51.groovy:6)

 

Any idea what I'm doing wrong?

 

2 answers

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
July 9, 2018

Hi,

If this is a post function then your code should be as follows. I'm going to assume that ID of your custom field is 11111 and its type is 'text (single line)'.

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def cf = cfManager.getCustomFieldObject((Long) 11111)
def user = userManager.getUserByName("username")

if (issue.getCustomFiledValue(cf).equals("value")){
watcherManager.startWatching(user, issue)
}
Ovidiu Vasilescu
Contributor
July 9, 2018

Hi Ivan,

 

Thanks for your reply. My field is a select list (single choice). I guess this is why I'm getting this error? 

chrome_2018-07-09_17-00-36.png

Thanks,

Ovidiu.

Ivan Tovbin
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.
July 9, 2018

Ah, I see. Then the code should be as follows:

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def cf = cfManager.getCustomFieldObject((Long) 11111)
def user = userManager.getUserByName("username")

if (issue.getCustomFiledValue(cf).getValue().equals("value")){
watcherManager.startWatching(user, issue)
}
Like Vladislav Khevuk likes this
Ovidiu Vasilescu
Contributor
July 9, 2018

Hi Ivan,

 

I'm getting the exact same error with this syntax.

chrome_2018-07-09_17-59-34.png

The cf is defined as you mentioned and it doesn't give any errors:

def cf = cfManager.getCustomFieldObject((Long) 11403)

 

Thanks,

Ovidiu.

Ivan Tovbin
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.
July 9, 2018

There's a typo on line 10. 

Instead of:

issue.getCustomFiledValue(cf).getValue().equals("value")

it should be:

issue.getCustomFieldValue(cf).getValue().equals("value")

 After that you can pretty much ignore other static type checking errors. It should work.

Ovidiu Vasilescu
Contributor
July 9, 2018

Thanks a lot Ivan, this works!

-Ovidiu.

0 votes
Roland Holban (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.
July 3, 2018

I assume you are trying this in a listener?

Ovidiu Vasilescu
Contributor
July 4, 2018

Sorry, didn't mention this part. Doing it as a post function as part of the create transition.

Suggest an answer

Log in or Sign up to answer