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! :)
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;
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
{code}
{code}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Yes, currently it also copies the custom fields without values.
The comment then looks like: "Name of customfield1: null"
regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.