The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

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 Champions.
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 Champions.
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.