Help with script for adding watchers on Create

Dharma Ramos
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.
October 25, 2019

Hello,

I have been reading a lot of community Q&A about using Scriptrunner to add a watcher on create based on a custom field. I put this together, added it after Creates the issue originally and after Re-Index and issue. It is not giving me an error, and there have been no failures on executions, but it is also not adding the watcher. Anyone care to opine? In case it matters, jira server v8.3.4 and Scriptrunner v5.6.2


import com.atlassian.jira.component.ComponentAccessor

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

def cfManager = ComponentAccessor.getCustomFieldManager()

def cf = cfManager.getCustomFieldObject("customfield_xxxxx")
def user = userManager.getUserByKey("uname")

def cfFieldValue = issue.getCustomFieldValue(cf)

if (cfFieldValue == 'Value'){
watcherManager.startWatching(user, issue)
}

1 answer

1 accepted

0 votes
Answer accepted
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 25, 2019

Hi @Dharma Ramos

you can try placing this function in the bottom of the order, also attaching my snippet for reference

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

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

if(issue.getSummary() == "Watcher Adding"){
ApplicationUser user = userManager.getUserByName("UsernName")
watcherManager.startWatching(user, issue)
}

 (I tried this snippet and it does works for me)

BR,

Leo

Dharma Ramos
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.
October 25, 2019

Tried it at the bottom and it did not work.

Is your code looking for the Summary? How do I define or get the value of a custom field using your suggestion?

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 25, 2019

Here you go for Custom Field value

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField= customFieldManager.getCustomFieldObjectByName("Custom Field Name")

def value = issue.getCustomFieldValue(cField)

if(value == "Something"){

   //Do something....

}

 

In your script you missed "ById" in getCustomfield method so the code is

def cf = cfManager.getCustomFieldObjectById("customfield_xxxxx")

 

BR,

Leo

Dharma Ramos
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.
October 28, 2019

So putting your suggestions together would look like this? It still did not work. Also, please note some comments in the script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField= customFieldManager.getCustomFieldObjectByName("Custom Field Name") //this line is giving me a warning to use customFieldManager.getCustomFieldObjectsByName instead

def value = issue.getCustomFieldValue(cField) //if I switch it to what it suggests above it says it cannot find this method.

if(value == "Something"){
ApplicationUser user = userManager.getUserByName("uname")
watcherManager.startWatching(user, issue)
}

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2019

Hi, 

You can ignore the warning message, or you can go with getCustomFieldById("customfield_xxxxx")

logger method and find out what you are getting the value in your script or try to log something on conditional loops(I don't find any errors)

Where you are placing this function. in the last of the order?

Like Dharma Ramos likes this
Dharma Ramos
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.
October 30, 2019

@Leo Thanks for all your help so far. I put it in the last step, after firing the event. Also, would the evaluation part need to be written differently if the field is a single select?

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2019

No, it's same like other fields. you can use the same along with as String >>>> issue.getCustomFieldValue(cField) as String

to get the value and validate it

Like Dharma Ramos likes this
Dharma Ramos
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.
October 31, 2019

That did it, I forgot the "as String" piece. Once I added that, it worked. Thanks @Leo !

Suggest an answer

Log in or Sign up to answer