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.
Community moderators have prevented the ability to post new answers.
You have to place a pair of braces "()" around the condition.
if (cfValues['Severity'].value == 'Critical')
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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." }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.