It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Help with script for adding watchers on Create

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 Oct 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

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 Oct 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

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 Oct 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

@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 Oct 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

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
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

237 views 1 2
Join discussion

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