Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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?
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.
How would I go about injecting the CustomFieldManager? Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
...
} }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use the field's string ID instead of name com.atlassian.jira.issue.CustomFieldManager.getCustomFieldObject(String)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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<CustomField> 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
A helpful answer!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use ComponentAccessor instead of ComponentManager if you can't inject CustomFieldManager.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) ")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's brilliant - thanks for your help. I've got it working now :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.