How to get a custom field value from a JIRA issue

Grant McConnaughey June 10, 2013

I am having trouble getting a custom field value from a JIRA issue. We have a "Customer" User Picker field attached to each issue that I need to get the value from. I saw some documentation showing I should use the ComponentManager class but when I try to import com.atlassian.jira.ComponentManager I get an error in IntelliJ (like it cannot find the class). Does anyone know what I should use to do this?

7 answers

1 accepted

3 votes
Answer accepted
Robert Jahrling
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.
June 10, 2013

Grant, you can inject a CustomFieldManager and then use one of the getCustomFieldObject methods (I've been using getCustomFieldObjectByName("Custom Field Name").) Once you have the CustomField object, use getValue(Issue issue) to get the field's value for the issue. Since getValue() returns an Object, you'll need to cast it.

Grant McConnaughey June 10, 2013

How would I go about injecting the CustomFieldManager? Thanks!

Robert Jahrling
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.
June 10, 2013

Something like this...

public class myClass {

  private final CustomFieldManager customFieldManager;

  public myClass(CustomFieldManager customFieldManager) {
    this.customFieldManager = customFieldManager;
  }

  public void myMethod() {
    ...
    CustomField customField = customFieldManager.getCustomFieldObjectByName("Custom Field Name");
    Object customFieldValue = customField.getValue(issue);
...
}
}

Like Arthur likes this
Sam Huawey December 25, 2015

Your answer is wrong. Custom field name is not unique and getCustomFieldObjectByName may return incorrect custom field, e.g. the one that is not configured for this particular project or issue type. This way the user of your code would receive null as value, instead of real value. Your code would work in a managed controlled environment, but if e.g. you develop a plugin for sell, you can not be sure if a customer would not make two or more fields with the same name. And your code will not work.

Like PietroLehmann likes this
Rusi Popov
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 13, 2016

Use the field's string ID instead of name com.atlassian.jira.issue.CustomFieldManager.getCustomFieldObject(String)

5 votes
Tony Filipe October 14, 2013

There are a few ways to get a CustomField's value in Jira:

CustomField.getValue(Issue)

(as previously suggested above) and

Issue.getCustomFieldValue(CustomField)

Both of these return Objects that then need to be cast. These are useful if you need to do something with the value (such as iterating over a list and operating on each item).

Another option for getting a CustomField's value is:

CustomField.getValueFromIssue(Issue)

This returns the value as it's rendered onscreen as a String. This can be handy to use when all you want is the String value (as was the case for me).

For Labels there's also:

LabelManager.getLabels(issueId, customFieldId)

which returns a Set<Label>.

3 votes
Sam Huawey December 25, 2015

 

I propose the following code - it is more robust than examples from above.

 

 

public static CustomField getCustomFieldByName(final Issue issue, final String name) {

        if (name == null || issue == null) // Something is wrong here, we don't care

            return null;

 

        Collection&lt;CustomField&gt; fields =  ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName(name);

 

        if (fields == null) // No field present, OK.

            return null;

 

        for(CustomField field:fields)

        {

            if(field.getRelevantConfig(issue) != null)

                return field;

            FieldConfig config = field.getRelevantConfig(issue);

            if(config != null)

                return field;

        }

 

        return null;

    }

 

 

and

 

CustomField field = getCustomFieldByName(issue, "Custom field");

  return field == null ? null : field.getValue(issue);

 

 Actually you may store the field variable for later use if you need to make bulk requests.

 

shun cao July 27, 2017

A helpful answer!

2 votes
Jobin Kuruvilla [Adaptavist]
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.
June 10, 2013

Use ComponentAccessor instead of ComponentManager if you can't inject CustomFieldManager.

0 votes
Steve Johnson September 24, 2020

This is the solution that worked for me

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

// Filed id : customfield_16306 --> This is field ID for Impact Score.
def cf = getFieldById("customfield_16306");
//int cFieldValue = (int)(cf.getValue())
def cFieldValue = (int)(cf.getValue() ? cf.getValue() : 0)

if (cFieldValue >= 1 && cFieldValue <= 100 )
{
cf.clearError()
}
else if (cFieldValue < 1 || cFieldValue > 100 )
{
//below Command displays Error and but allows the user to submit the change. If you are enabling below comment setError
//cf.setHelpText("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : "+ cFieldValue)

cf.setError("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : "+ cFieldValue)

}
else
{
cf.setError("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : (Blank) ")
}

0 votes
amiddleton June 22, 2014

That's brilliant - thanks for your help. I've got it working now :)

0 votes
amiddleton June 18, 2014

Sorry, I'm a bit confused. How do you create an instance of customFieldManager? The example class shows it being passed in and it's not clear to me. I have an issue object, and I want to get a custom field (of which I know the name) associated with it.

thanks

Tony Filipe June 22, 2014

That's the "injection" part of it - you don't need to instantiate a CustomFieldManager yourself, the development framework will automatically inject an instance of it into your class for you. All you have to do is list it as a parameter in your constructor.

When the development framework sees:

public myClass(CustomFieldManager customFieldManager) {

it will pass in an instance of CustomFieldManager that was previously created by Jira.

Robert Jahrling
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.
June 22, 2014

Right. Andy, all you have to do is declare a member of type CustomFieldManager as in line 3 of the earlier snippet, get the value that is injected and assign it to your member variable.

I used typical coding conventions, which can be a little confusing when you're not sure what you're looking at. Maybe this will make it a little clearer?

public class myClass {
 // Your local CustomFieldManager
  private final CustomFieldManager myCustomFieldManager;
// Class constructor requesting the injected CustomFieldManager
public myClass(CustomFieldManager injectedCustomFieldManager) {
    this.myCustomFieldManager = injectedCustomFieldManager;
  }
 
  public void myMethod() {
    // Other code here...

    CustomField customField = myCustomFieldManager.getCustomFieldObjectByName("Custom Field Name");
    Object customFieldValue = customField.getValue(issue);

   // ...and here.
  }
}

The developer documentation is a huge asset. This page gives a little bit more detail about Picocontainer: https://developer.atlassian.com/display/JIRADEV/PicoContainer+and+JIRA

Suggest an answer

Log in or Sign up to answer