Calculated Text field - concatenate comments

Wouter Franssens February 11, 2014

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
Answer accepted
David _old account_
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.
February 11, 2014

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();
 -->

Wouter Franssens February 13, 2014

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

David _old account_
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.
February 13, 2014

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();
 -->

Wouter Franssens February 13, 2014

Thanks David!

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

Wouter

Bharat Kavade November 4, 2015

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

David _old account_
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.
November 4, 2015

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.

Bharat Kavade November 5, 2015

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

0 votes
Wouter Franssens April 29, 2014

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

David _old account_
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.
April 29, 2014

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.

Wouter Franssens May 26, 2014

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

0 votes
David _old account_
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.
April 29, 2014

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

0 votes
Wouter Franssens April 28, 2014

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events