Calculated Text field - concatenate comments

Hello,

Is it possible to create a calculated text field that holds all the comments of that issue, each one started on an new line with the date and author in front of that comment?

If yes, what would be the code I have to enter in the description field?

Thank you,

Wouter

4 answers

1 accepted

2 votes
Accepted answer

You can start from the following snippet:

<!-- @@Formula: 
import com.atlassian.jira.issue.comments.CommentManager;
CommentManager commentManager = com.atlassian.jira.ComponentManager.getComponent(CommentManager.class);
StringBuilder s = new StringBuilder();
for (com.atlassian.jira.issue.comments.Comment c : commentManager.getComments(issueObject)) {
  s.append(c.getCreated().toString());
  s.append("\r");
  s.append(c.getBody()); 
  s.append("\r");
}
return s.toString();
 -->

Hello David,

This does the tric, thanks for that.

The only thing that isn't working is the append("\r"): both in Jira (5.2.4.1) as in the excel view, the concatenated string is on a single line. I suppose for the Excel view we would need something like "ALT-ENTER" ?

Another question: how can I change the format of .getCreated()? Now it shows for instance "Wed Feb 12 16:39:05 CET 2014". For our purposes, it suffices to show just "12/02/2014".

And finally: can I add also text formatting to this string (for instance: date in bold) I have tried this by appending Jira text effects to the string (*.....*), but this doesn't seem the way to go.

Thank you,

Wouter

I'm afraid the line breaks won't work correctly until I fix your last issue, which is related to the fact that Calculated Text Fields currently cannot use the wiki renderer. I'll fix that in the next version.

As for formatting dates, you can use the following:

<!-- @@Formula: 
import com.atlassian.jira.issue.comments.CommentManager;
import org.joda.time.DateTime;

CommentManager commentManager = com.atlassian.jira.ComponentManager.getComponent(CommentManager.class);
StringBuilder s = new StringBuilder();
for (com.atlassian.jira.issue.comments.Comment c : commentManager.getComments(issueObject)) {
 s.append( new DateTime( c.getCreated()).toString("dd/MM/yyyy"));
  s.append("\r\n");
  s.append(c.getBody()); 
  s.append("\n");
}
return s.toString();
 -->

Thanks David!

Question can be closed as far as I'm concerned.

Wouter

Hi David, I have used your code and it works well. However, it does not show the name or ID of the user who logged the comments. Is there a way to achieve the same? I looked up the CommentManager and also tried getCommentsForUser(), but it doesn't work. Will appreciate your help! -- Bharat K

Since "c" in the loop above is a Comment object, you can use c.getAuthorApplicationUser().getUsername() for the username, or c.getAuthorApplicationUser().getDisplayName() for the full name.

Thanks David. That did not work :) But the fault is mine. I should have mentioned that we are using v5.0.1 currently. The ApplicationUser class, of which getUsername() or getDisplayName() are the methods, is available from v5.1.1 onwards. However, I used c.getAuthorFullName() and it works just fine. We are planning to move shortly to v6.4.x or v7. When we do that, I'll have to change this call to what you have mentioned. But for now, it works like dream ! -- Bharat K

Hello David,

We are moving to JIRA 6.1.6. The simple calculated fields I create myself still work, but the one you created for me in the answer here above does not.

Do you have any idea what I need to change to make it work again?

Thank you,

Wouter

Did you look inside atlassian-jira.log for errors? Look for "com.innovalog.jmcf"

Hello David,

I did not find any errors with "com.innovalog.jmcf"

I did find some errors with "innovalog", they all look like this:

2014-04-24 13:38:03,557 JiraTaskExectionThread-2 ERROR 35kvae 811x2157x1 1vjc0qj 10.1.130.106 /secure/admin/jira/IndexReIndex.jspa [innovalog.jmcf.fields.CalculatedNumberField] CalculatedNumberField: error evaluating formula: Sourced file: inline evaluation of: `` issue.get("customfield_11604") * issue.get("customfield_11605") * issue.get("cu . . . '' : illegal use of null value or 'null' literal

They are not related to the custom calculated field I'm reffering to (which is "customfield_11700" and is not using any other custom fields).

Best regards,

Wouter

Well, if you're not getting any error, then I don't know. Try first replacing the formula with a simlpe constant to make sure that works, and then try re-adding code step-by-step until it breaks.

Hello David,

I tried your suggestion, and strangly, after re-adding all the original code, it still works. Probably a user error from my part.

I have an additional question with regards to formatting calculcated text fields, but I'll post this in a seperate topic.

Thanks,

Wouter

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,369 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you