How to get the Custom Field ID from a Custom Field Name in Java?

Shirley He March 1, 2018

Since  getCustomFieldObjectByName is now deprecated, getCustomFieldObject (either long or string) has to be used. I'm in a situation where I need to convert many custom field names into their IDs. One way I though of doing it was creating a Map where each custom field name is matched with its ID, however I feel that is a tedious way of doing things. Is there a way to convert the name of a custom field to its ID? I know there's a way to convert ID into name, but the reverse does not seem to exist unless I've missed something?

1 answer

1 vote
Alexey Matveev
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.
March 1, 2018

You can use getCustomFieldObjectsByName(String customFieldName). It returns a Collection of Custom Fields. 

Shirley He March 1, 2018

Ok, so I've tried that and added .iterator().next() to it, so now it looks like what I have below

customFieldManager.getCustomFieldObjectsByName(customFieldName).iterator().next()

Would this retrieve only the first customField listed in the collection, or would it go through the whole collection? I've checked and there shouldn't be any custom fields in the Jira instance I'm administering that have the same names, therefore there should only be one custom field in the collection.

I've tested the plugin in Jira, but I got this as an error, java.util.NoSuchElementException . Does this mean that the collection was empty from the start? Or that it looped through the collection and then encountered an error because it tried to iterate an empty collection?

Alexey Matveev
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.
March 1, 2018

It means that the collection was empty. There is no custom field with the name, which you set for the customFieldName variable. You should check your Collection for empty collection or check if it hasNext()

Suggest an answer

Log in or Sign up to answer