Add options to select field using RestAPI in Jira DC

Hi! I am using Jira DC v9 and trying to automate to add options to Issue Custom Field with type Select List (Single Choice). Unfortunately, there is no API for it in URL below. Does it still unavailable to do so?

https://docs.atlassian.com/software/jira/docs/api/REST/9.17.0/#api/2/customFields-getCustomFields

I don’t want to use Plugin. I just want to use RestAPI to do so. How could I to add options / edit field / edit contexts in Jira DC?

Hello @FelisCato111

You answered your own question when you said “Unfortunately, there is no API for it in URL below”. As you’ve already discovered, there are no API endpoints for changing custom field options for Jira DC.

Refer to JRASERVER-36112.

I see… I just expect someone would give me more detailed information like why this happend, or did I do somthing wrong, or there is updated documentation Google never know, as such the link you give to related ticket is really helpful.

Maybe I add my question a bit. I may try to read SDK technical guidelines and immitate it (which is bad practice). If I modify directly the database, on table customfields, would it be safe?

IMHO, the topic of whether or not it’s ‘safe’ to directly alter Jira’s database has been done to death. I recommend you Google ‘directly modify jira database’, read all the articles where other people have asked the same question in the past, then decide for yourself if it would be ‘safe’ or not.

Have fun. Bye.

Hello @FelisCato111,
i understand your frustration! Unfortunately, as of now, there isn’t a direct REST API endpoint in Jira Data Center (DC) v9 to add options to a Select List (Single Choice) custom field. This functionality is available in Jira Cloud but not yet fully supported in DC1.

However, as far as i know you should achieve this by using a combination of REST API calls and some scripting. Here’s a general approach:

  1. Get the Custom Field ID: First, you need to get the ID of the custom field you want to update. You can use the /rest/api/2/field endpoint for this.
  2. Get the Context ID: Next, you need to get the context ID for the custom field. You can use the /rest/api/2/field/{fieldId}/context endpoint for this.
  3. Add Options: Once you have the field ID and context ID, you can use the /rest/api/3/customField/{fieldId}/context/{contextId}/option endpoint to add options.

Here’s an example of how you might implement this in Java using the Atlassian SDK:

Java Example:

import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.RestClientException;
import com.atlassian.jira.rest.client.api.domain.CustomField;
import com.atlassian.jira.rest.client.api.domain.fieldtype.FieldType;
import com.atlassian.jira.rest.client.api.domain.fieldtype.SingleSelectList;
import com.atlassian.jira.rest.client.api.domain.fieldtype.SingleSelectListOption;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.atlassian.jira.rest.client.internal.async.jersey.JerseyJiraRestClientFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class JiraCustomFieldUpdater {
    public static void main(String[] args) {
        try {
            // Create Jira client
            JiraRestClient jiraRestClient = createJiraRestClient("https://your-jira-domain.atlassian.net", "username", "apiToken");

            // Get custom field ID
            CustomField customField = jiraRestClient.getCustomFieldClient().getCustomField("customfield_12345").get();
            System.out.println("Custom Field ID: " + customField.getId());

            // Get context ID
            URI contextUri = jiraRestClient.getContextClient().getContextForCustomField(customField.getId()).get().getContextUri();
            System.out.println("Context ID: " + contextUri);

            // Add options
            addOptions(jiraRestClient, customField.getId(), contextUri, "Option1", "Option2", "Option3");

        } catch (RestClientException e) {
            e.printStackTrace();
        }
    }

    private static JiraRestClient createJiraRestClient(String jiraDomain, String username, String apiToken) {
        URI baseUri = URI.create(jiraDomain);
        return new JiraRestClientFactory(new JerseyJiraRestClientFactory()).createWithBasicHttpAuthentication(baseUri, username, apiToken);
    }

    private static void addOptions(JiraRestClient jiraRestClient, String fieldId, URI contextUri, String... options) {
        List<SingleSelectListOption> optionList = new ArrayList<>();
        for (String option : options) {
            optionList.add(new SingleSelectListOption(option));
        }

        jiraRestClient.getContextClient().addOptionsForCustomField(fieldId, contextUri, optionList).execute();
        System.out.println("Options added successfully.");
    }
}

Steps to Use:

  1. Initialize the Jira Client: Create an instance of JiraRestClient with your Jira domain, username, and API token.
  2. Get the Custom Field ID: Call getCustomField with the name of your custom field.
  3. Get the Context ID: Call getContextForCustomField with the custom field ID.
  4. Add the Options: Call addOptions with the custom field ID, context ID, and the names of the options you want to add.

This should help you automate the process of adding options to your custom field in Jira DC v9.

Chears,
Daniel