We have a JIRA custom field called "Level of Effort", that has the following text values, selected from a Select List:
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?
Community moderators have prevented the ability to post new answers.
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.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.