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
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!!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
We are wanting to capture this information when a field is updated on the create and edit screen
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
Could you tell me the code to perform the same action but in script runner for jira cloud. Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.