Add watchers if custom field equals a value - Groovy Script

Eric Vincent March 9, 2014

I am using a groovy script to add users to the watch list of an issue (using post functions in workflow) but want to take it one step further. Can I add these watchers based on if a custom field equals a value? The custom field is a single select list. This is what I have so far:

import com.atlassian.jira.ComponentManager

def componentManager = ComponentManager.getInstance()

def watcherManager = componentManager.getWatcherManager()

def userManager = componentManager.getUserUtil()

if cfValues['Severity'].value == 'Critical'

{

def watchUsers = {usernames ->

   usernames.each {

         def user = userManager.getUser(it)

         watcherManager.startWatching(user,issue.getGenericValue())

      }

}

   def users = ["user1", "user2"]

   watchUsers(users)

}

It works fine when I remove the if statement, I am thinking I am missing something trival.

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Henning Tietgens
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.
March 9, 2014

You have to place a pair of braces "()" around the condition.

if (cfValues['Severity'].value == 'Critical')

Eric Vincent March 9, 2014

I have tried both solutions but still not working. I then tried to validate the condition using script runner:

It looks like it returns a true statement when running: cfValues['Severity'].value == 'Critical'

I think I might look into creating a listener to solve this problem but if you have any other ideas to why the "if" statement is not working, I would be most interested in hearing them. Thanks.

JamieA
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.
March 9, 2014

cfValues is only available in the Condition code, not in a general script.

If you check the log when running the script you posted in your question, you should see it failing because of the error Henning mentioned , and also because cfValues is undefined.

Henning Tietgens
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.
March 10, 2014

In case of a general script you could use something like this to get the value.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects()?.find{it.untranslatedName == 'My Custom Field'}
def selectFieldValue = null
if (cf) {
    selectFieldValue = issue?.getCustomFieldValue(cf)?.value
}
else {
    log.error "Customfield 'My Custom Field' not found."
}

0 votes
Andreas Ebert
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.
March 9, 2014

The value of a select custom field is not the visible text, but an ID. So instead of comparing cfValues['Severity'].value to "Critical" you have to compare it to the ID of the option named "Critical". Take a look at the html-source of a select custom field in any issue-edit-page. The options will look like this:

<option value="12345">Some Text</option>

The value is "12345", not "Some Text".

TAGS
AUG Leaders

Atlassian Community Events