You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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}"
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ricardo,
Thank you for confirming your issue has been resolved.
Regards,
Kristiaan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.