I'd like to add watchers when a custom field value is true

David Mason September 18, 2019

We have a custom field called 'Website' cf[11602] which is a checkbox selection with 3 choices.  I'd like to add 2 users as watchers if one particular checkbox is selected.  I've found a lot of old answers (pre Jira 7), but none of them seem to work.  This is the most recent post and seems fairly close to what I want to do. Could you help me modify this to work for my purpose?  Here's what I've been playing around with. I'm sure there are some imports the aren't necessary.  

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField

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


def user1 = userManager.getUserByName("user1")
def user2 = userManager.getUserByName("user2")
def customField = customFieldManager.getCustomFieldObject 'cf[11602]'

if(customField == 'Custom Field Value') {
watcherManager.startWatching(user1, issue)
watcherManager.startWatching(user2, issue)
}

 

I'm thinking...

def customField = customFieldManager.getCustomFieldObject 'cf[11602]'

...is the problem, but I'm not sure how to get what I need.  

 

it's failing with this error:

 

Time (on server): Wed Jun 27 2018 01:00:54 GMT-0400 (EDT)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-06-27 01:00:54,773 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-06-27 01:00:54,773 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITCM-7854, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script436
 at Script436.run(Script436.groovy:15)

and later...

So, this runs without error, but I don't get my watchers... :(

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField


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


def user1 = userManager.getUserByKey('importantUser1')
def user2 = userManager.getUserByKey('importantUser2')
def customField = CustomFieldManager.getCustomFieldObjectByName('MyCustomField')

if(customField == 'Custom Field Value') {
watcherManager.startWatching(user1, issue)
watcherManager.startWatching(user2, issue)
}

1 answer

0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 18, 2019

Hi @David Mason 

In your first code, there wasn't a line as below so that you were getting error: "No such property: customFieldManager for class" 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

Then in your second code snippet, you're checking the custom field object, not the custom field value of the issue.

It should be something like below

def customField = CustomFieldManager.getCustomFieldObjectByName('MyCustomField')

if('Custom Field Value'.equals(issue.getCustomFieldValue(customField))) {
watcherManager.startWatching(user1, issue)
watcherManager.startWatching(user2, issue)
}
David Mason September 25, 2019

I'll check it out and let you know!

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

Looking forward to it.

Tuncay Senturk
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 2, 2019

Hi @David Mason 

Have you tried yet? Did it make the trick? 

shivaram September 21, 2020

Hi @Tuncay Senturk ... I tried it from my side.. I still get the same error as david mason. It runs clean but i could not get any watchers for the ticket

 

Edited:

I can make the above script with changes work.. But 

 

if('CX'.equals(issue.getCustomFieldValue(customField))) {
watcherManager.startWatching(user2, issue)
}

 

This code can search for custom field saying taht custom field value would be CX only

It needs to "Contain" the value "CX" instead.. 

 

How to check the code whether it contains the value "CX" inside the custom field/??

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2020

Please try this one,

if(issue.getCustomFieldValue(customField).contains('CX')) {
watcherManager.startWatching(user2, issue)
}
shivaram September 28, 2020

Hi @Tuncay Senturk  Thanks for your help.. This command worked...

 

But this script works for the custom field text line only.

I have checkbox custom field in which if one custom field gets selected i needed the customfield value to put in here. For example, the selected custom field value is SPA , i needed the if condition to search for the value SPA  and add watchers..

 

It would be highly appreciated if you can help me with this

 

please and thanks,

Shiva ram

Suggest an answer

Log in or Sign up to answer