JIRA Cloud - API to update Component and Custom Fields of type Label

Roberto L
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.
August 1, 2021

Hello Community,

I have run into an issue where I am unable to correctly update the components field and any customfield of type Label.

Please find my code below

def componentValue = "XYZ" //XYZ is the name of my component
def versionTextField = "3.2"
def issueKey = "XY-123"

def update = put('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.body([
fields:[
components : [ { set: componentValue}],
customfield_10207 : [{ set: versionTextField }] //This customfield is a type Label field that I would like to add the value of versionTextField which is a String
]
])
.asString()

 

I have changed the variations of how I try to update components and customfield_10207 (Custom field of type label) but none have worked. If have been using the following CLOUD API documentation as help but it has not solved my issue Atlassian Cloud API Library, please navigate to "Edit issue" section.

 

The Version Text Field update attempt returns me the following error:

PUT request to /rest/api/2/issue/XYZ-123 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_10207":"Specify an string at index 0 for Version Text Field"}}
Serializing object into 'interface java.util.Map'

For the component update attempt I get the following error:

body: {"errorMessages":[],"errors":{"components":"Components is required."}}

 Components is required in my field configurations. Following that error message I made Components optional and it turns out that the code for Components works but it does not populate with the new component that exists rather it deletes the contents of Components fields, setting it to empty.

My understanding from the link above is that Components are updated via Component Names and not Component ID's. If it turns out I am wrong and I need to use Components ID would someone know the API to call to fetch the Component ID from a Component Name?

I appreciate your expertise and support!

-Roberto

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 1, 2021

Hi @Roberto Luces I think you mixed up "updage" and "fields" parameter.

If you want to simply set something and do not want to preserve previous values, you need to use this request:

{
"fields": {
"customfield_10056": ["a","b"]
}
}

If you want to preserve the values and want to add, remove values, you can use:

{
"update": {
"customfield_10056": [
{
"add": "c"
},
{
"remove": "b"
}
]
}
}

I just tested it with custom field of label type. Let me know if you need more help :) 

Roberto L
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.
August 3, 2021

Hello @Martin Bayer _MoroSystems_ s_r_o__ ,

 

Thank you for the above help! It has resolved my issue with the customfields of type label but unfortunately it does not work for the Component/s field.

 

I would like to update the Component/s field to have a value that I specifiy. I figured out how to fetch the ID of a component as long as I have the name. So I have available the Component ID for the given project the issue is in as well as the Name of the Component for the given project the issue is in.

My code runs, updates my customfield of type label with the correct value and does not return an error message but it does not update my issue component field. Find my code below.

def update = put('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.body([
fields:[
components: [ { id : component} ],
customfield_10207 : [versionTextField]
]

])
.asString()

 

Additionally, I gave it a go with your format of

components : [value, value2]

but JIRA would return me an error: 

body: {"errorMessages":[],"errors":{"components":"The components value must be an object"}}

-Roberto

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2021

Hi @Roberto Luces I just tried it and it works for me. I call the API from REST Client in my browser and body looks like:

{
"fields": {
"components": [
{
"id": "10019"
}
]
}
}

Can you share the whole script after you updated it? So I can copy it easily and test it? 

Suggest an answer

Log in or Sign up to answer