"Field <field name> cannot be set. It is not on the appropriate screen, or unknown." error with SL

riccardo.presutti January 8, 2020

I am using the Scriptrunner listener capability of Jira cloud version to update a custom field called "Last comment" when comment is updated/created on an issue. I get the following error:

 

2020-01-08 21:09:54.798 INFO - Serializing object into 'interface java.util.List'
2020-01-08 21:09:54.806 INFO - GET /rest/api/2/field asObject Request Duration: 604ms
2020-01-08 21:09:55.554 INFO - Serializing object into 'interface java.util.Map'
2020-01-08 21:09:55.558 INFO - GET /rest/api/2/issue/PP-151 asObject Request Duration: 715ms
2020-01-08 21:09:55.845 WARN - PUT request to /rest/api/2/issue/PP-151 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_10943":"Field 'customfield_10943' cannot be set. It is not on the appropriate screen, or unknown."}}
2020-01-08 21:09:55.846 INFO - PUT /rest/api/2/issue/PP-151 asString Request Duration: 284ms

Here is my script:

// Specify the issue key to update
def issueKey = issueKey

// Specify the name of the select list field to set
def selectListFieldName = 'Last comment'

// 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


// Fetch the issue object from the key
def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

// Get the comment
def commentField = (issue.fields as Map).comment as Map
def comments = commentField.get('comments') as Map
def mapSize = comments.size()

if (mapSize == 0) {
return
}

def lastComment = ((comments[mapSize-1]) as Map).body


def result = put("/rest/api/2/issue/${issueKey}")
 //.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
"${customField.key}":[value: "${lastComment}"] as Map
]
])
.asString()

if (result.status == 204) {
return 'Success'
} else {
return "${result.status}: ${result.body}"
}

 

3 answers

1 accepted

0 votes
Answer accepted
riccardo.presutti January 9, 2020

Thanks, the issue was resolved by creating the field with the new issue view.  

But now i'm getting another error:

{"customfield_10946":"Operation value must be a string"}}

I am using a multi-line text field

2020-01-09 14:33:45.439 INFO - Serializing object into 'interface java.util.List'
2020-01-09 14:33:45.445 INFO - GET /rest/api/2/field asObject Request Duration: 791ms
2020-01-09 14:33:45.918 INFO - Serializing object into 'interface java.util.Map'
2020-01-09 14:33:45.920 INFO - GET /rest/api/2/issue/PP-211 asObject Request Duration: 467ms
2020-01-09 14:33:46.118 WARN - PUT request to /rest/api/2/issue/PP-211 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_10946":"Operation value must be a string"}}
2020-01-09 14:33:46.119 INFO - PUT /rest/api/2/issue/PP-211 asString Request Duration: 196ms
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2020

Hi Ricardo,

Thank you for your response.

I can confirm this error means that the syntax of your rest call to set the field is incorrect as you not passing the data to be set in the structure that the rest API expects as it expects a string to be returned.

So that we can advise further on this can you please provide us with the code where you define the string to set as well as the rest call where you update the field so that we can advise on how to refactor it.

I can also confirm if you wish to set multiple lines of text then you will need to define a multi-line string inside of Groovy as described here.

Regards,

Kristian

riccardo.presutti January 9, 2020

Here is the code where I fetch the last comment and store it in <lastComment> to then put it in the custom field 

// Define the custom field
def customField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == selectListFieldName
} as Map


// Get the comment
def commentField = (issue.fields as Map).comment as Map
def comments = commentField.get('comments') as Map
def mapSize = comments.size()

if (mapSize == 0) {
return
}

def lastComment = ((comments[mapSize-1]) as Map).body


def result = put("/rest/api/2/issue/${issueKey}")
//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
"${customField.key}":[value: "${lastComment}"] as Map
]
])
.asString()
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2020

Hi Ricardo,

I can see from the line of "${customField.key}":[value: "${lastComment}"] as Map in your code that you passing the value as a map to the field and the error expects you should be passing the value as a String.

Also, you may need to convert the value you have extracted from the comment field and into a Multi-Line String which is defined as shown in the link shared previously so that you can pass the value to be set in the result put request as a String.

Could you please try setting the value as a String to see if this resolves the error which you are seeing.

Regards,

Kristian

riccardo.presutti January 9, 2020

Thanks for your help, everything is working fine.

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2020

Hi Ricardo,

Thank you for confirming your issue has been resolved. 

Regards,

Kristiaan

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2020

Hi Ricardo,

Thank you for your question.

I can confirm from your response that you are getting that the error indicates that ScriptRunner for Jira Cloud cannot set the custom field with the ID of customfield_10943 as it is not on the create and edit screens for the project.

By default, the plugin can only set fields which are on the issue screen and if you wish to set fields which are not on the screen then you will need to add the line of .queryString("overrideScreenSecurity", Boolean.TRUE) to your rest API call and run your script as the ScriptRunner Add-On User as this will allow the field to be set even tho it is not on the issue screen as described in the documentation page located here.

I hope this helps.

Kristian

0 votes
David_Bakkers
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.
January 8, 2020

ScriptRunner uses the REST API, which in turn needs the field to be available on the issue's Edit screen. To check this, open one of the issues you are trying to update the Last Comment field on, then click the Edit button in the corner to open the Edit screen.

Can you see / edit the Last Comment field there? If not, add it to that screen and try again.

Suggest an answer

Log in or Sign up to answer