How to Copy Custom field value to Comments using Script Runner

Christian Roestel Community Champion Mar 11, 2016
We are Trying to copy a custom field value to the comment when the custom field is updated. 
does anyone have an example of the script they would be willing to share? 

3 answers

This widget could not be displayed.

Hi Christian,

I can confirm that this requirement can be accomplished by the ScriptRunner plugin.

Could i please check if you wish to do this in a workflow post function or when a field is updated on the create and edit screens using the Behaviours module of ScriptRunner?

Thanks

Kristian

Christian Roestel Community Champion Mar 21, 2016

We are wanting to capture this information when a field is updated on the create and edit screen

This widget could not be displayed.

Hi Christian,

As i have not heard back from you recently i have attached an example script for a post function that will take the value out of a Text Field and add this as a comment on the issue during the transition.

This code could be modified to take the values from other fields apart from text fields if required.

Code:

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

// Get a pointer to my Demo Text Field
def textField = customFieldManager.getCustomFieldObjectByName("Demo Text Field");

// Make sure the text field is not null and if so add its value as a comment
if(textField){
    // Set the CommentText as the value of my text field
    String CommentText = textField.getValue(issue).toString()
    // Add a comment to the Issue
    commentManager.create(issue,CurrentUser,CommentText,true)
}

 

I hope this helps

Kristian

Christian Roestel Community Champion Mar 21, 2016

this is great but the our concern is that we want to check if Custom field ""Demo Text Field"  content changed when the issue was updated or changed.   could you do me the extra step and let me know how I can check current value vs. previous value? 


Thanks in advance!!!!

 

Hi Christian,

You should look at the answer provided on the question here by Jamie which should give you the code that will enable you to detect if the field value has changed during in the transition.

You could then modify the if condition to only add the comment if the values are different.

I hope this helps

Kristian

Hello Kristian,

Thanks for this code comments that help me to understand JIRA programming.

Since 2016, some functions has been deprecated.

Can you help me to update it please ?

So far I have the following code :

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
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 = ApplicationUser.getUsername();

// Get the current Issue
Issue issue = issue;

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

// Get a pointer to my Demo Text Field
def textField = customFieldManager.getCustomFieldObjectByName("En attente d'un tiers");

// Make sure the text field is not null and if so add its value as a comment
if(textField){
// Set the CommentText as the value of my text field
String CommentText = textField.getValue(issue).toString();
// Add a comment to the Issue
commentManager.create(issue,CurrentUser,CommentText,true);
}

The last function that has issue is the last one :

commentManager.create(issue,CurrentUser,CommentText,true);

I get the following error message :

create(Issue issue, String author, String body, boolean dispatchEvent)

Deprecated. 

Use create(Issue issue, ApplicationUser author, String body, boolean dispatchEvent) instead. Since 6.0.

 

Thanks in advance for any help !

This widget could not be displayed.

hello,

you might find this link useful:

https://answers.atlassian.com/questions/268243

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

565 views 25 15
Join discussion

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