How to get the selected value from a custom field select type using Jelly Script?

Hi,

I want to get the selected value of a select type custom field and assign/set that value to another select type custom field using Jelly Script.

Appreciate any help. Thanks!

2 answers

1 accepted

Let's start with the easy part. If you were creating a new issue, you could assign a value to a custom field for that new issue as described in the JIRA Jelly Tag reference documentation.

Since you ask about getting the current value of a custom field, I assume that you want to update an existing issue rather than create a new one. That's not quite so easy because that capability is not provided directly by the JIRA Jelly tags. Instead, you must use some tags in the jelly:core tag library to interact with the JIRA components. The code snippet below shows how you can obtain the value of a custom field called MyField. Hold on tight, because this gets a little hairy.

<!-- Get an instance of ComponentManager -->
<core:invokeStatic className="com.atlassian.jira.ComponentManager" method="getInstance" var="componentManager"/>

<!-- Get CustomFieldManager from ComponentManager -->
<core:invoke on="${componentManager}" method="getCustomFieldManager" var="customFieldManager"/>

<!-- Get CustomField object from customFieldManager -->
<core:invoke on="${customFieldManager}" method="getCustomFieldObjectByName" var="myField">
    <core:arg type="java.lang.String" value="My Field"/>
</core:invoke>

<!-- Get IssueManager from ComponentManager -->
<core:invoke on="${componentManager}" method="getIssueManager" var="issueManager"/>

<!-- Get the Issue from IssueManager -->
<core:invoke on="${issueManager}" method="getIssueObject" var="issueKey">
    <core:arg type="java.lang.String" value="${issue.key}"/>
</core:invoke>

<!-- Get Custom Field Value -->
<core:invoke on="${myField}" method="getValue" var="myFieldValue">
    <core:arg type="com.atlassian.jira.issue.IssueImpl" value="${issueKey}"/>
</core:invoke>

So there you have it. We're interacting with components that are part of JIRA's public API from within the script.

As of JIRA 5.2, most of the ComponentManager methods are deprecated, in favour of the ComponentAccessor. That makes our lives a git easier. If you are working with JIRA 5.2 and later, to get a reference to the CustomFieldManager, do this:

<!-- Get an instance of ComponentManager -->
<core:invokeStatic className="com.atlassian.jira.component.ComponentAccessor" method="getCustomFieldManager" var="customFieldManager"/>

To set the value of a custom field in an existing issue, you'll need to work with the OrderableField interface, which is implemented by CustomField; specifically the updateValue method. I'm sure you can work out the core:invoke commands for that.

Hi,

don't now anything about jelly ;-)

But I have used a custom field in my velocity templates these days:

$customFieldManager.getCustomFieldObjectByName("Overall_start")

Maybe this helps!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Thursday in Off-topic

Friday Fun: Magic Eyes

...staring into the background. Once the image pops out in 3D, you can look around the picture and enjoy. If you will see if you are a true illusion master! :) You did it? :) Wow! Awesome! As a bonus...

455 views 83 11
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you