how to get customfield value in python jira api

이창호 August 29, 2019

Hi

I have custom field in jira, and I want get value in custom field

 

example in jira

custom field : week

custom field value : monday, tuesday, wednesday, thursday, friday, saturday, sunday

custom field id : customfield10003

 

example in python

after jira login

customFieldValue = jira.fields.customfield10003.value

or

customFieldValue = jira.fields.week.value

print(customFieldValue)

output print: [monday, tuesday, wednesday, thursday, friday, saturday, sunday]

 

but it's not working

 

how can I work this code?

 

thank u

2 answers

1 accepted

6 votes
Answer accepted
Rafael Pinto Sperafico
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 30, 2019

Hi @이창호 ,

Based on the example you have provided, it seems you are using https://jira.readthedocs.io/en/master/ . If so, you could do something similar to:

# Fetch all fields
allfields = jira.fields()
# Make a map from field name -> field id
nameMap = {field['name']:field['id'] for field in allfields}
# Fetch an issue
issue = jira.issue('ABC-1')
# You can now look up custom fields by name using the map
getattr(issue.fields, nameMap[custom_name])

Please, make sure the custom field you are trying to get is visible and searchable in the Jira Issue. You can run a REST API call against a particular issue key (e.g ABC-1) reviewing if the custom field you are after is available:

https://docs.atlassian.com/software/jira/docs/api/REST/7.12.0/#api/2/issue-getIssue

GET /rest/api/2/issue/{issueIdOrKey}

Kind regards,
Rafael

이창호 August 30, 2019

thank for your answer

i tried

allfields = jira.fields()
idnameMap = {jira.field['name']:jira.field['id'] for jira.field in allfields}
getValues = allfields(issue.fields, idnameMap["customfield_10012"])

but it's not working

where did I go wrong?

이창호 August 30, 2019
allfields = jira.fields()

nameMap = {jira.field['name']:jira.field['id'] for jira.field in allfields}

getvalue = getattr(issue.fields, nameMap["week"])
print(getvalue)
#I wanted output : monday, tuesday, wednesday, thursday, friday, saturday, sunday
#but this code output is : monday

 

I tried your code

but i got the just one value

I need all value in field

thank you

Rafael Pinto Sperafico
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 30, 2019

Hi @이창호 ,

You forgot to search for an issue:

issue = jira.issue('ABC-1')

Output idnameMap to the console and review if the custom field you are after (e.g customfield_10012) is present in the object.

이창호 August 30, 2019

I excuted code when after search issue

issue = jira.issue('EX-221')
allfields = jira.fields()
nameMap = {jira.field['name']:jira.field['id'] for jira.field in allfields}
getvalue = getattr(issue.fields, nameMap["week"])
print(getValue)
#output "monday"
Rafael Pinto Sperafico
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 30, 2019

Hi @이창호 ,

Based on comment:

"#I wanted output : monday, tuesday, wednesday, thursday, friday, saturday, sunday
#but this code output is : monday"

What you want is not the customField value but the options available to the custom field. Therefore, you should be looking at metadata instead.

Here is an example on how to get metadata from Select List (cascading) com.atlassian.jira.plugin.system.customfieldtypes:cascadingselect:

from jira import JIRA
import re

options = {'server': 'http://localhost:8080/jira'}
jira = JIRA(options, basic_auth=('admin', 'admin'))

# get an example issue that has the field you're interested in
issue = jira.issue("ABC-1")

# get the metadata
meta = jira.editmeta(issue)

# this is the customField I have created
# Select List (cascading) com.atlassian.jira.plugin.system.customfieldtypes:cascadingselect
allowedValues = meta['fields']['customfield_10300']['allowedValues']

for options in allowedValues:
print(options['value'])

Reference: https://confluence.atlassian.com/jirakb/how-to-retrieve-available-options-for-a-multi-select-customfield-via-jira-rest-api-815566715.html

Kind regards,
Rafael

이창호 August 30, 2019

oh..... that's right what I need!

thank you!

이창호 August 30, 2019

I have one more question T.T

if have child data in customField

{'id': 'customfield_10003', 'key': 'customfield_10003', 'name': 'week'
'schema': {'type': 'option-with-child', 'custom': 'com.atlassian.jira.plugin.system.customfieldtypes:cascadingselect', 'customId': 10003}}

like 

customField : week

data : monday, tuesday, wednesday, thursday, friday, saturday, sunday

child data : hour, minuit, second

allowedValues = meta['fields']['customfield_10300']['allowedValues']
print(allowedValues )
# output : monday, tuesday, wednesday, thursday, friday, saturday, sunday
# and I need more data : hour, minuit, second

how can I get with child data in customField?

0 votes
Warren
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 30, 2019

Hi @이창호 

You haven't said whether this is a server or cloud instance, although I'm not sure that it makes much difference.

I think that you need an underscore "_" after field so that it becomes

customFieldValue = jira.fields.customfield_10003.value
이창호 August 30, 2019

thank for your answer

i writed undersocre when I try in python scrypt

but it's not working too

Like Xu Xinkai likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events