Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner condition if value is present in custom label field Edited

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?

1 answer

1 accepted

3 votes
Answer accepted
Nir Haimov Community Leader Sep 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.

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

Nir Haimov Community Leader Sep 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
}

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 Sep 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 Jul 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

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you