Add Author and comment date/time at the end in a last comment custom field.

Ward Schwillens, Schwillie
Contributor
March 11, 2024

Hi fellow 

I have a script that adds the last comment to a custom field called "last comment"

I also add the comment author to the last comment author custom field, and the comment created date/time into a last comment created date/time field. 

But in the overview on a dashboard, using a filter result, this tends to get quite wide. 

Therefor I want to add the comment author and comment created date time at the bottom of the last comment custom field, preferably on a new line and with a title: "Author : " and "Comment Created Date/time : " before the actual data. 

 

How can I do that? 


//Last update by Ward Schwillens at 11/03/2024 11.32
final ProjectReportCommentFieldName = 'Project Report Comment'
final lastProjectReportCommentAuthorFieldName = 'Last Project Report comment author'
final lastProjectReportCommentDateTimeFieldName = 'Last Project Report comment date/time'
def titleAuthor = 'Author :'
def titleDateTime = 'Date/Time :'

// Get custom fields properties
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>
// Get ProjectReport comment custom field id
def ProjectReportCommentCfId = customFields.find { it.name == ProjectReportCommentFieldName }?.id
// Get last issue comment author custom field id
def lastProjectReportCommentAuthorCfId = customFields.find { it.name == lastProjectReportCommentAuthorFieldName }?.id
// Get last issue comment date/time custom field id
def lastProjectReportCommentDateTimeCfId = customFields.find { it.name == lastProjectReportCommentDateTimeFieldName }?.id

// Updates the issue with the created comment body
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(ProjectReportCommentCfId): comment.body
]
])
.asString()

// Updates the issue with the created comment author
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[

(lastProjectReportCommentAuthorCfId): comment.author
]
])
.asString()

// Updates the issue with the created comment date/time
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(lastProjectReportCommentDateTimeCfId): comment.created
]
])
.asString()

I tried stuff like this but that did not work: 

// Updates the issue with the created comment body
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(ProjectReportCommentCfId): comment.body + " " + comment.author + " " + comment.created
]
])
.asString()

Any ideas? Thank you for your insight!

2 answers

1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2024

Hi Ward,

What type of field is last comment if its a text field to add new lines you will need to use a new line character of \n.

The page at https://www.baeldung.com/java-string-newline gives examples of how to add new lines to strings in Java and this is what you will need to do.

Also I would advise constructing the string with the new lines in a variable outside of the request to update the field and then just pass this variable when updating the field.

I hope this helps.

Regards,

Kristian

0 votes
Hannes Obweger - JXL for Jira
Atlassian Partner
March 23, 2024

Hi @Ward Schwillens, Schwillie

just as food for thought: Depending on your exact requirements, there may be apps available in the Atlassian Marketplace that can provide a simpler and more optimised solution for what you're looking for.

If you'd be open to this, you may want to have a look at the app that my team and I are working on, JXL for Jira.

JXL is a full-fledged spreadsheet/table view for your issues that allows viewing, inline-editing, sorting, and filtering by all your issue fields, much like you’d do in e.g. Excel or Google Sheets. It also comes with a number of so-called smart columns that aren’t natively available, including the last comment, date of last comment, last commented by user, and many other comment-related columns.

This is how it looks in action:

last-comment-fields.gif

As you can see above, you can easily sort and filter by your comment-columns, freely arrange them as it suits you, and also use them across JXL's advanced features, such as support for (configurable) issue hierarchies, issue grouping by any issue field(s), sum-ups, or conditional formatting.

This all just works - there's no scripting or automation whatsoever required.

Any questions just let me know,

Best,

Hannes

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events