Groovy script for copying custom fields as a comment

Alexander Naumann September 20, 2018

hello,

I have a groovy script that copies the values of custom fields into a comment.
But the whole is very static. (including the names of the custom fields)

it should now be adjusted so that it only copies the custom fields, which also have a value.
See the current script below. (I know it's not perfect, sorry, guys!)

use case info: it is a service desk project where bugs are reported. then the issues moved by an owner into a normal jira project of the corresponding team. Therefore the values of the custom fields should be copied into a comment (because the custom fields are not in the normal projects)

// Required Imports
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getUser().name

// Get the current Issue
Issue issue = issue

// Get the Manager classes required
def CommentManager commentManager = ComponentAccessor.getCommentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// List of Custom Fields for Comment the CommentText as the value of my text field
String CommentText1 = "Name of custom_field1: " + customFieldManager.getCustomFieldObject("customfield_123").getValue(issue).toString() + "\n\n"
String CommentText2 = "Name of custom_field2: " + customFieldManager.getCustomFieldObject("customfield_124").getValue(issue).toString() + "\n\n"
String CommentText3 = "Name of custom_field3: " + customFieldManager.getCustomFieldObject("customfield_125").getValue(issue).toString() + "\n\n"
String CommentText4 = "Name of custom_field4: " + customFieldManager.getCustomFieldObject("customfield_126").getValue(issue).toString() + "\n\n"
String CommentText5 = "Name of custom_field5: " + customFieldManager.getCustomFieldObject("customfield_127").getValue(issue).toString() + "\n\n"

//Create Comment with all informations
CommentTextSummary = CommentText1 + CommentText2 + CommentText3 + CommentText4 + CommentText5
commentManager.create(issue,CurrentUser,CommentTextSummary,true)

I minimized the script to five custom fields.
Can someone help me?

Thank you for your answers and help! :)

 

3 answers

1 accepted

1 vote
Answer accepted
Orkun Gedik
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.
September 20, 2018

Hello @Alexander Naumann,

I think, I did not completelly understand what are you asking for. Therefore, let me know if I misunderstood you. As far as I understood, you want to modify your script as follows;

  • You do not want to get all customfield one by one. 
  • You do not want to add a comment  into CommentTextSummary if  the comment is empty.

So, if these are your cases, you can use following script. Hope this helps.

// Required Imports
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField;

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getUser().name

// Get the current Issue
Issue issue = issue

// Get the Manager classes required
def CommentManager commentManager = ComponentAccessor.getCommentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

List<CustomField> customFields = customFieldManager.getCustomFieldObjects(issue)
String commentTextSummary = ""

for (CustomField customField : customFields){
if(customField.getValue(issue)!= null && customField.getValue(issue) != ""){
commentTextSummary += customField.getName() + " : " + customField.getValue(issue).toString() + "\n\n"
}
}
commentManager.create(issue,CurrentUser,CommentTextSummary,true)

Regards,

Orkun Gedik

Alexander Naumann September 21, 2018

HI @Orkun Gedik,

thanks for your reply :)
You understood correctly. The cases you mention, are the ones I want.

A question about your script: Do I specify the customfields in "List <CustomField>"? If yes, how exactly?
Because when I copy and test the script, Groovy throws me an error:

groovy.lang.MissingPropertyException: No such property: CommentTextSummary for class: script1537532946657371027539

Thank you very much for your help!

Regards,
Alex

Orkun Gedik
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.
September 21, 2018

You are welcome @Alexander Naumann,

Sorry for my bad there is a typo in the script. Can you change the last line with the following line please :)

commentManager.create(issue,CurrentUser,commentTextSummary,true)
Alexander Naumann October 10, 2018

Hey @Orkun Gedik,

sorry for the late feedback, we had a national holiday and I had a few days vacation.

Now the script works, thanks!
But... ;) now it copies all custom fields (if they have a default value) we have in JIRA and systems fields like watchers.
Do you have any idea how to prevent this?

I have currently solved it so, that I hide the custom fields with default values via the field configuration scheme and exclude system fields in the script as follows:

for (CustomField customField : customFields){
if(customField.getValue(issue)!= null && customField.getValue(issue) != "" && customField.getName() != "Watchers" ){
commentTextSummary += customField.getName() + " : " + customField.getValue(issue).toString() + "\n\n"
}
}

That works for me and i'm fine with it :)

Thank you!

0 votes
ti
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 27, 2019

Dear,

I would like to configure a post function to copy a custom field result to comment. I can use any plugin to do this?

Bhanu
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 14, 2023

{code}

// Required Imports
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.CustomFieldManager

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get the Manager classes required
def CommentManager commentManager = ComponentAccessor.getCommentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def commentText = issue.getCustomFieldValue("Name of the customfield").toString()

//Add Comment
commentManager.create(issue,CurrentUser,commentText,true)

{code}

0 votes
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2018

Hi,

Sorry, but i didn't understand your requirement.

You said the copy works for you.

Now you want the copy to work only for fields with values?

Currently it copy the fields that also empty?

Alexander Naumann September 21, 2018

Hi,

Yes, currently it also copies the custom fields without values.
The comment then looks like: "Name of customfield1: null"

regards

Suggest an answer

Log in or Sign up to answer