Simple put custome firled value with Jira Cloud

Shawn Mahoney March 25, 2019

Not sure why this documentation is so hard to find. I simply want to update a custom field to a value.

This is in Jira Cloud.  How can I set the customfield_10121 to the dropdown value of ENV1? 

 

def customFieldName = 'customfield_10100'

def result = get("/rest/api/2/issue/${issue.key}?fields=${customFieldName}")
.header('Content-Type', 'application/json')
.asObject(Map)

def type = result.body.fields[customFieldName].value
if (type == 'New Functionality') {
def setEnvironment = put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
customfield_10121: 'ENV1'
]
])
.asString()
}

1 answer

1 accepted

0 votes
Answer accepted
Aleksandr Zuevich
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 25, 2019

Hi,

 

I suppose you get Could not find valid 'id' or 'value' in the Parent Option object error.

Just update this line:

fields: [
customfield_10121:[value: 'ENV1']
]

Shawn Mahoney March 26, 2019

I found the answer in another snippet in the scripting console.  You need to find the option value first.

 

 

// Specify the name of the select list field to set

def selectListFieldName = 'Assigned Environment'

 

// Get the Custom field to get the option value from

def customField = get("/rest/api/2/field")

        .asObject(List)

        .body

        .find {

    (it as Map).name == selectListFieldName

} as Map

// Check if the custom field returns a valid field and is not null

assert customField != null : "Cannot find custom field with name of: ${selectListFieldName}"

def result = put("/rest/api/2/issue/${issue.key}")

        .header('Content-Type', 'application/json')

        .body([

        fields: [

                (customField.id):[value: "ENV1"] as Map

        ]

])

        .asString()

if (result.status == 204) {

    return "The ${customField.name} select list field was successfully updated on the ${issue.key} issue"

} else {

    return "${result.status}: ${result.body}"

}

Suggest an answer

Log in or Sign up to answer