It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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

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()

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

Thanks for your help, everything is working fine.

Hi Ricardo,

Thank you for confirming your issue has been resolved. 

Regards,

Kristiaan

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.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Next-gen

Next-gen Customer Council

Hey Next-gen fans, Would you like the opportunity to give direct feedback to the Next-gen product team? Would you like the chance to preview and test new Next-gen features? I’m excited to annou...

1,023 views 13 26
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you