Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Comparing collections with groovy

Steven Mustari
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.
October 13, 2020
// Get the values of the Product field
def cf_product = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Product")
def product = cf_product.getValue(issue) as List

//Products teams are responsible for
def droptableProducts = ["Image Scaling / Cleanup", "OCR", "Extraction", "Recognition", "Annotation", "Text Classification", "Purge", "Solarity Services - Legacy (Java)", "Group Separation", "Model Builder", "Validation (TP)"]

...Any usual list comparison is failing at this point...

Is there something special I'm missing about pulling in list data from a multiselect customfield?

I've tried intersect, disjoint, and iterating with .any and .every and all methods seems to be failing. And by failing I mean returning unexpected results.

Case:

The value "Annotation" is in the product field.

def commonValue = product.intersect(droptableProducts)

If I return product I get "[Annotation]" as expected.
If I return droptableProducts I get the list as expected.
If I return commonValue, I get an empty list.

Help! :D

1 answer

1 accepted

1 vote
Answer accepted
Jeroen Poismans
Community Champion
October 22, 2020

Hi Steven,

I'm going to try and point your nose in the (what I think) right direction.

You have 2 List you are trying to compare: dropTableProducts and products. In order to sucessfully compare the items in Lists, the contents need to be of the same type or need to be comparable to each other.

Although this might seem to be the case because the product List seems to contains the value "Annotation" in your example, it does not. What you see when you print out the value of that List is the String representation of that "Annotation" option. The actual type of List product is => class com.atlassian.jira.issue.customfields.option.LazyLoadedOption

You can validate this by doing this:

// Get the values of the Product field
def cf_product = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Product")
def product = cf_product.getValue(issue) as List

return product.get(0).getClass()

This will return the type of the data in your first element of the product  List.

Your second List (dropTableProducts) is of type String. Comparing those 2 Lists will give very weird results, since there is no way for groovy to know how to compare en Option with a String.

Solution:

From the products customfield get the String values of the selected options and put them in a List. Then compare that List with your dropTableProducts. That should work.

Let me know if this helped or if you have any more questions!

 

Regards,

 

Jeroen

Steven Mustari
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.
October 23, 2020

Jeroen,

Thank you for the detailed explanation. This makes sense to me now, I appreciate the assistance!

Regards,
Steven

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.1
TAGS
AUG Leaders

Atlassian Community Events