Compare multi-version picker w/single version picker

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

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.

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?
 

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

251 views 2 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you