Hello-
We migrated from Server to Cloud, there are few scripted field which needs recreation. I am looking to recreate Last Comment Scripted field with below -
final lastCommentFieldName = 'Last Comment'
// Get last comment custom field id
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>
def lastCommentCfId = customFields.find { it.name == lastCommentFieldName }?.id
// Updates the issue with the created comment body
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(lastCommentCfId): comment.body
]
])
.asString()
Result :-
groovy.lang.MissingPropertyException: No such property: issue for class: Script1 at Script1.run(Script1.groovy:12) at Script1$run.call(Unknown Source) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:35) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:29) at ConsoleScriptExecution1_groovyProxy.run(Unknown Source)
Let me know what's missing
def issueKey = issue.key
def response= get("/rest/api/2/issue/${issueKey}")
.header("Content-Type", "application/json")
.asObject(Map)
.body
response["fields"]["Last Comment"]
Try this
The scripted field ran successfully, but returned an invalid type. Please return a string (no newline characters allowed)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your response, found a solution --
// Get the comments off the issue as a list
def commentsList = issue.fields?.comment?.comments
// Check if the last comment is not null.
if (commentsList) {
// If comments exists return the last comment and remove any new line characters to make it a valid return type
return commentsList.last()?.body.toString().replaceAll("\r", " ").replaceAll("\n", " ");
} else {
// If no comments exist then display some default text.
return "No comments exist on the issue"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have just quoted an example of using the rest API and custom fields.
Good to hear that you found the solution as per your requirement.
Kudos.
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.