Forums

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

Last Comment Scripted Field

Satya Prakash Anand March 5, 2024

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 

    

1 answer

0 votes
Uday Kiran Bhaviri
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.
March 5, 2024

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

Satya Prakash Anand March 5, 2024

Invalid type

The scripted field ran successfully, but returned an invalid type. Please return a string (no newline characters allowed)

Satya Prakash Anand March 5, 2024

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"
}

Like Uday Kiran Bhaviri likes this
Uday Kiran Bhaviri
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.
March 6, 2024

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.

Suggest an answer

Log in or Sign up to answer