Compare multi-version picker w/single version picker

Rose Cruz September 14, 2016

Similar to the question about Groovy Script compare version picker, I want a scripted field to compare two custom version picker fields within an issue. One field is a multi-select version picker (exists in) and the other is a single-select version picker (found in). Both are required and will always have a value.

If any of the version IDs listed in “ExistsIn” are less than the single version ID set for “FoundIn”, the result should be true.

I couldn’t figure out how to calculate "ExistsIn" < “FoundIn”, so I tried this, which kind of works:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField FoundIn = customFieldManager.getCustomFieldObject("customfield_11800");
CustomField ExistsIn = customFieldManager.getCustomFieldObject("customfield_10007");
 
if (ExistsIn != FoundIn) return "Yes"
else 
return null

 

The issues are still reindexing, but it looks l like they’re all being set to Yes, even when the value in both fields is identical. sad

As an added challenge, I want to exclude issues that have a certain version ID set for “ExistsIn”. (ExistsIn != “17407”)

Any help would be appreciated!

1 answer

0 votes
Jonny Carter
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.
September 14, 2016

Right now, you're comparing the custom field objects themselves. You'll want to use FoundIn.getValue(issue) and ExistsIn.getValue(issue) to get the actual value of those fields.

After that, checking for your pre-determined version ID should be pretty straightforward.

Rose Cruz September 14, 2016

Thanks Jonny! Getting closer. I got the version picker comparison working with this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField FoundIn = customFieldManager.getCustomFieldObject("customfield_11800");
CustomField ExistsIn = customFieldManager.getCustomFieldObject("customfield_10007");


if (FoundIn.getValue(issue) != ExistsIn.getValue(issue))
return "Yes"
else
return null

I'm still struggling to get the version ID condition working.

I tried using this, using both the version ID and the version name:

if (ExistsIn.getValue(issue) != "17407")
return "Yes"
else
return null

The issue preview is returning "Yes" for issues where ExistsIn = 17407. What am I doing wrong, and how can I combine this condition with the FoundIn != ExistsIn clause?
 

Jonny Carter
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.
September 15, 2016

Version Picker fields will return a VersionImpl object when the getValue method is called. The name property has the text that appears in JIRA, the id has the numeric ID as a Long. Use them as appropriate. Calling toString on it will coerce it to its name as a string.

By the way, for comparing two version numbers, there's some good ideas for Java which would port to Groovy. My favorites are just tokenizing the strings or using Maven's comparator. Similar groovy-specific solutions exist as well. That presumes your naming convention follows the "Major.Minor.Revision" style. If you're using "Version 1.0", "Version 2.3", etc. style, you'll need something different.

Suggest an answer

Log in or Sign up to answer