How can i Search the Issue of an Custom field with Java?

Onder Yesil February 25, 2013

From another system I get a custom field value X and I want use this value to search the Issue of this custom field. How can I do this with java. Do you have a example?

1 answer

1 vote
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

I'm not sure of the structure of your custom field and it's values, but it sounds to me like you may need to write a searcher for it. See https://developer.atlassian.com/display/JIRADEV/JIRA+Plugin+Guide and look for the customfield-searcher module

If the field was a simple string, date, number, etc, and stored normally in Jira like the standard custom fields, you could simply use the most appropriate built-in searcher. But I've a feeling that won't work for your field.

Onder Yesil February 25, 2013

I have next cod to get the CustomField:

String genesisID = "genesisID";
CustomField genesisIDField =    fieldManager.getCustomField(genesisID);
           
 Object genesisIDValue = genesisIDField.getCustomFieldType().getValueFromIssue(genesisIDField, [WHAT IS HIS ISSUE])

But, I need some code to search the ISSUE ID of the CustomField with value "X".

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

Sorry, you've lost me. That code is out of context - I don't know where you're running it, or quite why.

Start at the beginning. What TYPE is the field? How does it store it's data?

Onder Yesil February 25, 2013

I get from another system a custom field(name = genesisID) with value X. I want use value X to get the ISSUE of Custom Field. What i knos is the name of Customfield and the Value of Custom Field.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

I'm afraid that's just repeating yourself instead of answering the question.

Could you tell us what TYPE of field you are working with. What TYPE is the field called genesisID?

Once we know that, we will be in a better place to understand "I want to use value X to get the issue of custom field" - that itself sounds wrong to me though - custom fields belong to issues. We'd expect you to be asking "what value is stored for custom field X on issue Y" or "I need to search for issues that have customfield value Y in custom field X", but "use value X to get issue of custom field" just does not make sense.

Onder Yesil February 25, 2013

I need to search for issues that have Customfield value Y in Custom field x. That is what i want. I have only Customfiield value Y and Customfield X.

Onder Yesil February 25, 2013

The Type is an Text-Field

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

For the third time. Please answer the question

What TYPE is custom field X ?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

Ok, so, can you search this field as a human using the standard ui? For example, using the advanced search, you put in something like:

genesisID ~ "XYZ-123"

and it finds all the issues where genesisID contains XYZ-123 as a string?

Onder Yesil February 25, 2013

Yes, it finds all the issues. How do i this with java?

Onder Yesil February 25, 2013

Thank you. How can I get the key ID (like 10010L) of genesisID? From DataBase or Jira UI?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

Good, that means the field has a text searcher associated with it correctly, which gets us most of the way there.

Depends on the context of where you are doing it and why, but here's a starter:

  • final JqlQueryBuilder myJQL = JqlQueryBuilder.newBuilder();
  • myJQL.where().customField(10010L).like("XYZ-123");
  • Query query = myJQL.buildQuery();
  • final SearchResults results = searchService.search(authenticationContext.getUser(), query, PagerFilter.getUnlimitedFilter());

Note that the 10010L in the second line is the custom field's key id, so you'll need to change that to whatever it is for genesisID

That will get you a "search results" object which has some useful info about your search, including a list that contains each issue returned:

  • List<Issue> issues = results.getIssues();
Onder Yesil February 25, 2013

thank you very much, the information is useful! field manager has no method "getCustomFieldByName (" genesisID ")". I use Jira 5.1.8.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

You could get it by reading the database - table is customfield.

Jira UI exposes it too - hover the cursor over the admin links or go into one of them for the field, and you'll see an id=xxxxx, or customfield_xxxxx in the urls exposed.

In code, there are functions like fieldManager.getCustomFieldByName("genesisID")

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2013

Sorry, I should have said that was a bit of a guess about getCustomFieldByName - I've done it in my own code, but I didn't remember exactly how. It's actually customFieldManager.getCustomFieldObjectByName(fname)

Suggest an answer

Log in or Sign up to answer