How to Copy Custom field value to Comments using Script Runner

C
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.
March 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? 

4 answers

2 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2016

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

C
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.
March 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!!!!

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2016

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

FP October 25, 2017

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 !

1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 12, 2016

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

C
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.
March 21, 2016

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

0 votes
Milena Gonzalez March 31, 2020

Hi

 

Could you tell me the code to perform the same action but in script runner for jira cloud. Thanks

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2020

Hi Milena,

Thank you for your response.

I have created a code sample located here which can be run on the Script Console inside of ScriptRunner for Jira Cloud to copy the value from a single line text field to a comment.

This script will provide an example for ScriptRunner for Jira Cloud which you can take and modify to copy the values of different field types to a comment. 

I can confirm one way to see how the extract the values for different field types is to navigate to the  page and to click on the text which says Get Issue Fields in the Examples section below the code box.

Running this script will allow you to return the JSON structure for issues to see how fields are stored, in order to help you determine what structure you need to use to extract the data from the field.

You can then modify this script to return the field you require in order to see how to extract the data from this field.

I hope this helps

Regards,

Kristian

0 votes
Elifcan Cakmak
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.
March 11, 2016

hello,

you might find this link useful:

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

Suggest an answer

Log in or Sign up to answer