Snippets

Adaptavist Jira Cloud Script Field to Get Last Comment text and display it on an issue

Updated by Kristian Walker

File JiraCloudLastCommentScriptedField.groovy Modified

  • Ignore whitespace
  • Hide word diff
 // 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\n", " ");
+    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"
Created by Kristian Walker

File JiraCloudLastCommentScriptedField.groovy Added

  • Ignore whitespace
  • Hide word diff
+/*
+* This example script field script should configured to display on the 'Issue Sidebar' and shows how to get all comments on the issue and then to get the last comment and to display the text from this inside a script field. 
+* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
+* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
+* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
+*/
+
+// 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\n", " ");
+}else{
+    // If no comments exist then display some default text.
+    return "No comments exist on the issue"
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.