Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to access CustomField value using rest-API

Rai, Jagrit February 15, 2023

How can i access the particular customfield fields value like I want to get approver field accountId value then how can we access that.

question.png

1 answer

0 votes
Jeroen Poismans
Community Champion
February 15, 2023

Hi,

Basically, what the rest call resturn is a JSON response.

First, try and do the call in Postman or another client where you can inspect the result.
The response might look something like this for the Approvers field:

 {
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations","id": "10083",
"self": "https://<your-site>.atlassian.net/rest/api/3/issue/10083",
"key": "AUTBL-26",
"fields": {
"customfield_10003": [
{
"self": "https://<your-site>.atlassian.net/rest/api/3/user?accountId=557058%3Afbb39111-6fb5-4fa3-acca-2807f563c6df",
"accountId": "<the account ID here>",
"emailAddress": "someEmail@email.com",
"avatarUrls": {
"48x48": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/557058:fbb39111-6fb5-4fa3-acca-2807f563c6df/55d30f27-0da5-45da-a670-91e420673421/48",
"24x24": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/557058:fbb39111-6fb5-4fa3-acca-2807f563c6df/55d30f27-0da5-45da-a670-91e420673421/24",
"16x16": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/557058:fbb39111-6fb5-4fa3-acca-2807f563c6df/55d30f27-0da5-45da-a670-91e420673421/16",
"32x32": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/557058:fbb39111-6fb5-4fa3-acca-2807f563c6df/55d30f27-0da5-45da-a670-91e420673421/32"
},
"displayName": "Jeroen Poismans",
"active": true,
"timeZone": "Europe/Brussels",
"accountType": "atlassian"
}
]
}
}


 As you can see the Approvers field returns an array of objects. So let's try this:

def customfields1 = customFields[0].accountId

Hope this points you in the right direction!

Jeroen

Rai, Jagrit February 15, 2023

yes i tried in this way too but in post-function script runner it shows warning like thisquestion2.png

Jeroen Poismans
Community Champion
February 15, 2023

Ok, never mind the type error and lines ... Those are probanly there because of type checking.

I saw an error in my code, try something like this:

def customfields1 = customFields.fields.customfield_10507[0].accountId 

 

Regards,

Jeroen

Rai, Jagrit February 15, 2023

No Jeroen there is still type checking errors exist  question3.png

btw why you are accessing 0th index of array object however the approver field account id is at first index i think.

Jeroen Poismans
Community Champion
February 15, 2023

Hi,

Never mind the type checking warnings, try executing the code (maybe in script console with an example issue) and see what real errors come out of it then ...

Now, if you inspect the JSON from my first reply you will see that the customfield contains an array. Inside that array the first Approver object is at index 0, not 1.

 

Regards,

Jeroen

Rai, Jagrit February 15, 2023

question5.png

these are the error shows on execution question4.png

Jeroen Poismans
Community Champion
February 15, 2023

Hi,

Try this, this I have tested myself:

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

if (result.status == 200){
return result.body.fields?.customfield_10507[0].accountId
} else {
return "Failed to find issue: Status: ${result.status} ${result.body}"
}

It's the part after the return statement that contains the data you want.

 

Regards,

Jeroen

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events