Forums

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

Error changing custom field value using python python-jira

Mike Kavalchuk November 30, 2018

I have created a new custom field to introduce form controls for better operational measurability, however, i want to map all existing label values to the new field "allocation". There are several thousand issues I need to update and was trying to test on this one ticket.

 

My Code: 

 

from jira import JIRA

issue = jira.issue('Project1-1855')


allocation_field = issue.fields.customfield_16542.value

existinglabel_field = issue.fields.labels
existinglabel_field_string = str(existinglabel_field[0])

print ('want to change this...'
, allocation_field
,'to this...'
, issue.fields.labels[0]
)


#Set New Allocation Value
issue.update(fields={allocation_field: {'name': existinglabel_field_string}})

print ('The allocation field value of:'
, allocation_field
, 'has been changed to:'
, issue.fields.labels)

 

The error code i am getting is :

JIRAError: JiraError HTTP 400 url: https://ourcompany.jira.com/rest/api/2/issue/141717
	text: Field 'NewAllocationField' cannot be set. It is not on the appropriate screen, or unknown.

 

This custom field is associated to all the different screens, screen schemes, and issue type screen schemes in the project. I know this because I can see the field when I naviagate to https://ourcompany.jira.com/secure/admin/ConfigureFieldScreen.jspa?id=XXX  Can someone please advise?

 

1 answer

1 accepted

0 votes
Answer accepted
Mike Kavalchuk December 5, 2018

To provide a long winded answer to my issue...

The main issue here was that I was taking the labels (which is a free form text field) and trying to map them to values in a drop-down list. This meant that I had to create a dictionary mapping label values to individual field ID Values (each potential drop down item has a ID associated to the value) We discovered this by inspecting the HTML of the drop down list.

 

solution below (which also allows for looping through all issues between specified ticket numbers):

 

allocation_field = 'customfield_16542'

reference_dict = {
"LABEL1" : {
"id": "5001",
"value": "ALLOCATION1"
},
"LABEL2" : {
"id": "5002",
"value": "ALLOCATION2"
},
"LABEL3" : {
"id": "5003",
"value": "ALLOCATION3"
},
"LABEL4" : {
"id": "5004",
"value": "ALLOCATION4"
},

}

 

#Specify ticket number range here for all issues you want to update

for i in range(914, 1880):
try:
issue = jira.issue('PROJECT-{}'.format(i))

# Will there ever need to be more than one label?
label = issue.fields.labels[0]
except:
# Issue with assigning label or finding issue
print("Issue PROJECT-{} was skipped".format(i))
logging.warning("Issue PROJECT-%s was skipped", i)
failure_list.append("PROJECT-{}".format(i))
continue

if label in reference_dict.keys():
print('Issue {} was updated'.format(issue.key))
logging.debug("Issue %s was updated", issue.key)
success_list.append(issue.key)

 

issue.update(
fields={
allocation_field: {
'id': reference_dict[label]['id'],
'value': reference_dict[label]['value']
}
}
)

except:
print("Issue {} failed to update".format(issue.key))
logging.warning("Issue %s failed to update", issue.key)
failure_list.append(issue.key)

 

else:
# No label match
print('Issue {} was NOT updated'.format(issue.key))
logging.warning("Issue %s was NOT updated", issue.key)
failure_list.append(issue.key)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events