Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Groovy script for copying custom fields as a comment

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.
Sep 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

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.
Sep 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)

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!

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.
Nov 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.
Sep 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?

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