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

ScriptRunner Script Field based on text value from another custom field

JamesT
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.
December 27, 2016

We have a JIRA custom field called "Level of Effort", that has the following text values, selected from a Select List:

  • XS
  • Small
  • Medium
  • Large
  • XL

We're using this because some users don't get Story Points.  However, it can be very useful to treat those text values as numeric values (for sprints, for sorting, etc.), so I'm trying to create a ScriptRunner custom Script Field.

Here's the code:

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Level of Effort")
def effortText = issue.getCustomFieldValue(field);
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug("YYY: ${effortText}")
if (effortText == "XS") { return "1" }
else if (effortText == "Medium") { return "3" }
else { return effortText }

OK, I have a few more "else if" lines to add (or a "case" if I can figure how to do that in Java), but the problem is that the "if effortText" lines never evaluate as `true`.  Instead, the final else always triggers, and returns the custom field's value.

I had to switch the custom field template to "Text Field" instead of a numeric template so that I could debug it using the Preview function instead of the JIRA log, but once the code works I'll swap out that last line out for a 'return 0" or something.  But, that last line (and the log statement too) confirm to me that "effortText" does indeed have the values I'm checking for (XS, etc.) and I don't see any extra whitespace in the Preview output.

So, the "effortText" value seems to match the values I'm checking for ("XS", etc.), but those "if" lines are not triggering.  Any ideas why?

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
JamesT
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.
December 27, 2016

Figured it out right after posting the question, even though I had been struggling with this for over an hour.

Needed to change:

 

def effortText = issue.getCustomFieldValue(field);

to:

 

def effortText = issue.getCustomFieldValue(field).toString();

Would welcome any comments explaining why getCustomFieldValue() is not returning a string, but seemed to be returning a "com.atlassian.jira.issue.customfields.option.LazyLoadedOption" instead.

JamieA
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.
January 2, 2017

Well, because it does. For select lists and radio buttons you will get an https://docs.atlassian.com/jira/server/com/atlassian/jira/issue/customfields/option/Option.html.

For multi-selects and checkboxes you get a Collection of Options.

This allows you to get the option IDs, and child options (for cascading selects), etc.

To get the string value call .value on it (toString() happens to be implemented to return the value too).

JamesT
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.
January 3, 2017

Thanks for the explanation.  I guess I never would have have named a function getCustomFieldValue() if it wasn't actually returning the value.  It's really more of a getCustomFieldObject() function...

TAGS
AUG Leaders

Atlassian Community Events