How to automatically add options to a single-choice select list using Scriptrunner in Jira Cloud.
Asked by Tim Juniel:

Problem: {#toc-hId--349582379}
------------------------------

I want to automatically add custom field options for a single-choice select-list using scriptrunner.

I have already written the following script, which is based on this page:

[PUT-Update custom field options (context)](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-custom-field-options/#api-rest-api-3-field-fieldid-context-contextid-option-put)

Script: {#toc-hId-2137930454}
-----------------------------

```
import groovy.json.JsonSlurper

import groovy.json.JsonOutput


// Jira Configuration

def baseUrl = "[Jira URL]"

def apiToken = "[My API Token]"

def email = "[My Username]"


// Authentication

def authString = "${email}:${apiToken}".bytes.encodeBase64().toString()

def headers = [

    "Authorization": "Basic ${authString}",

    "Content-Type": "application/json",

    "Accept": "application/json"

]


// Data for the POST request

def bodyData = '''

{

  "options": [

    {

      "disabled": false,

      "value": "Scranton"

    },

    {

      "disabled": true,

      "optionId": "10000",

      "value": "Manhattan"

    },

    {

      "disabled": false,

      "value": "The Electric City"

    }

  ]

}

'''


// Function to execute an HTTP request

def sendHttpRequest(urlStr, method, headers, body = null) {

    def url = new URL(urlStr)

    def connection = url.openConnection() as HttpURLConnection

    connection.requestMethod = method

    headers.each { k, v -> connection.setRequestProperty(k, v) }

    if (body) {

        connection.doOutput = true

        connection.outputStream.withWriter { it << body }

    }

    def responseCode = connection.responseCode

    def responseMessage = connection.responseMessage

    def responseText = connection.inputStream.text

    return [responseCode: responseCode, responseMessage: responseMessage, responseText: responseText]

}


// ID of the field and context

def fieldId = "customfield_12345"

def contextId = "56789"

def url = "${baseUrl}/rest/api/3/field/${fieldId}/context/${contextId}/option"

def response = sendHttpRequest(url, 'POST', headers, bodyData)


// Output the API response for debugging

println "Response Code: ${response.responseCode}"

println "Response Message: ${response.responseMessage}"

println "Response Text: ${response.responseText}"


// Parse the JSON response (if necessary)

def jsonSlurper = new JsonSlurper()

def jsonResponse = jsonSlurper.parseText(response.responseText)

println JsonOutput.prettyPrint(JsonOutput.toJson(jsonResponse))

```

#### This script throws the following error: {#toc-hId-1031540569}

java.io.IOException: Server returned HTTP response code: 400 for URL: ...

With a GET query I don't get this error and it gives me the context values. What could be the reason that I cannot add any options, although they can also be read.

Thanks in Advance,

Tim


Answers:
author: Sean Chua _Adaptavist_
body: Hey [@Tim Juniel](/t5/user/viewprofilepage/user-id/5569798) , welcome to Atlassian Community!

<br />

Since your GET call is working, I am assuming that you do know how to get your CustomField ID and Context ID for the options editing.

<br />

If you are using ScriptRunner for Jira Cloud's [Script Console](https://docs.adaptavist.com/sr4jc/latest/features/script-console), you can probably try something like;  

def customFieldId = "customfield_XXXXX"  
def contextId = "XXXXX"   
def newOptionValue = "whatever new option"

def requestBody = \[  
options: \[  
\[value: newOptionValue\]  
\]  
\]

def response = post("/rest/api/3/field/${customFieldId}/context/${contextId}/option")  
.header("Content-Type", "application/json")  
.body(requestBody)  
.asObject(Map)

println "Response Status: ${response.status}"  
println "Response Body: ${response.body}"

Hope that helps.


Tags: automation

cloud

custom-field

jira-cloud

jira-service-management

jira-service-management-cloud

scriptrunner

select-list