Custom field value to comment

Hi All,

we got a difficult request from a project lead: they want to copy the value of a custom field to comment. I.e. on each issue they would like to create a comment with the value of this custom field. The field is of free text type.

Now we don't know a method how to do this. Have you ever done such? Any idea?

Thanks in advance!

Rumi

5 answers

1 accepted

This widget could not be displayed.

Hi,

I think that you can do this with JJUPIN or JJS scripting plugin, or you develop a plugin.

In jira 4.1.2 , we can do this with Javascript

This widget could not be displayed.

The tools currently provided within JIRA do not provide a solution for this, not have I been able to find a plugin on our marketplace that provides such functionality. I can't directly see the purpose of doing this, perhaps you can explain more on why exaclty you want to have this functionality in place, maybe we can find another (better fitting) solution.

This widget could not be displayed.
Mizan Community Champion Apr 28, 2013

You can write a groovy script which will

-> get all issues of the project

-> iterate these issues and get the value of the field for each issue .

-> add comment programmatically where value will be the customfield value

You can refer the in built script https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-CopyCustomFieldValues

EDIT : you will need to run this script in the script runner panel and test on a test instance before you apply it to the live instance

This widget could not be displayed.

Hi,

I have a script which saves the issue key of each issue to comment.

How can it be changed if we want to save the value of a custom field to comment?

<?xml version="1.0"?>
<!--
// ===========================================================================
// Writes an issue key as a comment
// This version of the script checks if previously an issue key was already
// saved in one of the comments. This is slower but it is handy when a
// previous run has crashed.
// ---------------------------------------------------------------------------
// Author...........: Arpad Madocsai
// Last change date.: 2012-11-01
// Last change by...: George Lewe
// ===========================================================================
-->
<JiraJelly xmlns:jira="jelly:com.atlassian.jira.jelly.enterprise.JiraTagLib" xmlns:core="jelly:core" xmlns:log="jelly:log" >
 
   <log:info>Running Jelly script: Issue key saver</log:info>
  
   <!-- Set the project key -->
   <core:set var="projectId" value="10092" />
   
   <core:invokeStatic className="com.atlassian.jira.ComponentManager" method="getInstance" var="componentManager"/>
   
   <!-- Get CommentManager and IssueManager from ComponentManager -->
   <core:invoke on="${componentManager}" method="getCommentManager" var="commentManager" />
   <core:invoke on="${componentManager}" method="getIssueManager" var="issueManager" />
  
   <!-- Get all issue IDs from this project -->
   <core:invoke on="${issueManager}" method="getIssueIdsForProject" var="issues">
      <core:arg type="java.lang.Long" value="${projectId}"/>
   </core:invoke>
    
   <!-- Loop through all issues -->
   <core:forEach var="issueId" items="${issues}">
  
      <!-- Get the issue -->
      <core:invoke on="${issueManager}" method="getIssueObject" var="issue" >
         <core:arg type="java.lang.Long" value="${issueId}"/>
      </core:invoke>
  
      <!-- Get the issue's comments -->
      <core:invoke on="${commentManager}" method="getComments" var="comments">
         <core:arg type="com.atlassian.jira.issue.Issue" value="${issue}"/>
      </core:invoke>
      
      <!-- Check if the key already is in one of the comments -->
      <core:set var="found" value="false" />
      <core:forEach var="comment" items="${comments}">
         <core:invoke on="${comment.body}" method="startsWith" var="found">
            <core:arg type="java.lang.String" value="Pre-migration issue key"/>
         </core:invoke>
      </core:forEach>
  
      <!-- If no previous key was found then add it now -->
      <core:if test="${!found}">
         ${issue.key}
         <core:invoke on="${commentManager}" method="create" var="newcomment">
            <core:arg type="com.atlassian.jira.issue.Issue" value="${issue}"/>
            <core:arg type="java.lang.String" value="u204497"/>
            <core:arg type="java.lang.String" value="Pre-migration issue key: ${issue.key}"/>
            <core:arg type="boolean" value="false"/>
         </core:invoke>
      </core:if>
  
   </core:forEach>
         
</JiraJelly>

This widget could not be displayed.

Our use case for this functionality was a system wide un-changing identifier that could be shared with external parties who don't care 'where' the issue is in JIRA but rather wanted to leverage the quicksearch option to rapidly/easily locate an issue (that you knew the global key of beforehand).

So we created a simple GUID plugin that used existing JIRA sequence rules to create a Support ID that wouldn't change. At which point everyone went 'hey, great, now how do I find it via quicksearch?'.

The solution was a Groovy Listener that took the generated GUID value and added it as the first comment to the issue. The groovy code we used is as follows - it wasn't as classy or sophiscated as is could have been but it was a nice 12 hour problem to solution stretch so everyone was generally happy.

NOTE: We had to use a listener instead of a post-function on issue create due to how you cannot control the order post functions are executed - the listener just made it simpler.

package com.awesomenesscanbeuncool.jira.groovy.listeners
/**
 * Created: 20130101.2358
 */

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import org.apache.log4j.Logger

public class AddCommentToEnableStaticSupportIDQuickSearchListener extends AbstractIssueEventListener 
{

    Logger log = Logger.getLogger(AddCommentToEnableStaticSupportIDQuickSearchListener.class);

    // -- ------------------------------------------------------------------ --
    // -- Defines
    // -- ------------------------------------------------------------------ --
    // CF_STATICSUPPORTID is the name of the custom field
    public static final String CF_STATICSUPPORTID = "GUID";
    // DATA_MIGRATION_USER is the username of the user used to migrate data into JIRA from external systems
    public static final String DATA_MIGRATION_USER = "datamigration.user";


    void issueCreated(IssueEvent event) 
    {
        log.debug("Issue Created - calling addComment");
        addComment(event)
    }

    void addComment(IssueEvent event) 
    {
        // Set the listenerIssue variable
        // issue is supplied by the groovy script engine automatically
        Issue listenerIssue = event.getIssue();

        // Get the remote user
        // use the user object returned by getJIRAAuthenticationContext as it won't need to be converted
        User currentUser = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();

        // Don't add the comment if the issue is being migrated from an external system
        if (currentUser != null && !DATA_MIGRATION_USER.equals(currentUser.getName())) 
        {
            CommentManager commentManager = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class);
            CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

            String comment;
            CustomField cf = customFieldManager.getCustomFieldObjectByName(CF_STATICSUPPORTID);
            if (cf != null)  
            {
                String value = listenerIssue.getCustomFieldValue(cf);
                comment = "-->Static Support ID: " + value + "<--";
            } 
            else 
            {
                comment = "CustomField " + CF_STATICSUPPORTID + " cannot be found";
            }

            if (comment != null) 
            {
                commentManager.create(listenerIssue, currentUser.getName(), comment, false);
            }
        }
    }
}

----

Because of the specific situation we added other unique'ing type symbols around the ID just in case the ID was referenced by a user in another issue comment.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

47 views 0 1
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