Scriptrunner condition if value is present in custom label field

Martin Hohenberg September 19, 2018

Hi.

I am trying to create a scriptrunner post-function based on the presence of a certain label in a *custom* label field.

This is the condition that I have come up with based on other answers:

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.customfields.option.Option;

CustomFieldManager customFieldManager = (CustomFieldManager)ComponentAccessor.getCustomFieldManager()
CustomField custom_field = customFieldManager.getCustomFieldObjectByName( "myCustomLabelField" );
def my_labels = (List<Option>) issue.getCustomFieldValue(custom_field);

if (my_labels != null && my_labels.contains("myTriggeringValue")) {
    return true
} else {
    return false
}

... but it does not seem to trigger (but also does not throw an error). Has anyone ever done this before?

2 answers

1 accepted

3 votes
Answer accepted
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 19, 2018

Hi,

What is "stream_labels"?

in your first "if" you use it, but you never declared it.

From your code, seems to me like you always get "return false" that's is why it's not working for you and why you also not getting errors.

Martin Hohenberg September 20, 2018

I'm sorry, I anonymized the script, but forgot about that one. Corrected the source.

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2018

my_labels is a collection of labels (Object), not Strings, so you can't check "contains" on it.

This complete code will work for you, i tested it myself:

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;

CustomFieldManager customFieldManager = (CustomFieldManager)ComponentAccessor.getCustomFieldManager()
CustomField custom_field = customFieldManager.getCustomFieldObjectByName("myCustomLabelField");
def my_labels = issue.getCustomFieldValue(custom_field);
List<String> labelsList = new ArrayList<>(my_labels.size());

for (Object object : my_labels) {
labelsList.add(Objects.toString(object, null));
}

if (my_labels != null && labelsList.contains("myTriggeringValue")) {
return true
} else {
return false
}
Martin Hohenberg September 21, 2018

That seems a lot saner and actually helps me understand the basic problem - but there still seems to be an issue with this code.

Apparently, this line does not work:

List<String> labelsList = new ArrayList<>(my_labels.size());

It seems like ArrayList cannot handle Objects?

JIRA version is 7.4.2, if this is relevant.

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 21, 2018

With the line you mentioned, and the for i wrote right after, the collection of objects (my_labels) is converted to the List<String>

for (Object object : my_labels) {
labelsList.add(Objects.toString(object, null));
}

it is 100% work because i use the same thing and it works for me 

Moses Thomas
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 16, 2021

@Nir Haimov  when you debug, 

for (Object object : my_labels) {
labelsList.add(Objects.toString(object, null));

display result for log.error("Please show ---> $labelsList") this give collection of string, and when you get the CustomFieldValue for labels,  this is already a collection(List)  so  you dont need the code above which will make the code not to even work. I guess this  this  is why @Martin Hohenberg  is complaining.

Kind regards,

Moses

0 votes
David Harkins
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.
February 16, 2024

You could simplify this a little by using a find:

 

testIssue.getLabels().find( label -> label.getLabel() == 'A-Label')

Suggest an answer

Log in or Sign up to answer