It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to copy values from two fields into summary by script

Indranil Mondal Jun 21, 2016

I would like to copy values from 2 custom fields eg 'category' and 'name' into one system field summary. There are some post functions or script runner which copies from one to one or one to many but I need to copy from many to one. In other words I want to copy values from 'category'  and 'name' fields and concatenate those two values to get the title of the issue. As title comes from summary, I guess values have to copied into summary field first. Its possible by 'workflow toolbox' plugin but as purchasing plugin may not be allowed I want to do this by some scripts groovy or anything else that can be put into post functions.

All helps will be appreciated.

4 answers

1 vote
Vasiliy Zverev Jun 21, 2016

This postfucntion should be before standard "store to database". Here is code for scripted postfunction provided by ScriptRunner plugin:

package UpdateIssue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue

/**
 * Created by VZverev on 22.06.2016.
 */

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZVER-16")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

issue.setSummary(customFieldManager.getCustomFieldObjectByName("category").getValue(issue).toString() + "/" + customFieldManager.getCustomFieldObjectByName("name").getValue(issue).toString())
Indranil Mondal Jun 21, 2016

Hi vasilly,

Thanks for you quick response. Want to confirm one thing, should I replace "ZVER-16" with my issue type name? and can I put it in the post function of the intended transition of that issue type?

Vasiliy Zverev Jun 21, 2016

Sorry, this is just for test into Script console, user this:

package UpdateIssue
 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
 
issue.setSummary(customFieldManager.getCustomFieldObjectByName("category").getValue(issue).toString() + "/" + customFieldManager.getCustomFieldObjectByName("name").getValue(issue).toString())
Indranil Mondal Jun 22, 2016

I am getting only null values in place of values of those 2 custom fields. result is coming as "null / null"

Vasiliy Zverev Jun 22, 2016
  1. Make sure that custom field for given issues are not empty
  2. Make sure that custom fields names are correct

Hi Vasiliy,

Post function is in the work flow and your code is to do update one issue at a time. In case of a requirement to update a custom field of every issue in a project with value from another field in bulk, would you suggest an easy way? For example, copy the value of Component/s field into another custom field called 'old component'. Thanks.

Vasiliy Zverev Jun 24, 2016

So, then you need 

  1. Male a loop on all requared issues
  2. manually store changes to a database

Here is modified code for script console provided by ScrptRunner plugin:

package UpdateIssue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser

/**
 * Created by VZverev on 22.06.2016.
 */

IssueManager issueManager = ComponentAccessor.getIssueManager();

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cstFld1 = customFieldManager.getCustomFieldObjectByName("category")
CustomField cstFld2 = customFieldManager.getCustomFieldObjectByName("name")

ApplicationUser curUser = ComponentAccessor.getJiraAuthenticationContext().getUser();

UpdateIssueRequest updateIssueRequest = new UpdateIssueRequest(new UpdateIssueRequest.UpdateIssueRequestBuilder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false))

for(Project project: ComponentAccessor.getProjectManager().getProjectObjects())
    for(MutableIssue issue:issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.getId()))) {

        issue.setSummary(cstFld1.getValue(issue).toString() + "/" + cstFld2.getValue(issue).toString())

        //store to a database
        issueManager.updateIssue(curUser, issue, updateIssueRequest)
    }
0 votes

Why do you want to do this? I have seen customers use a single field such as Summary for two pieces of information. Later on they want to split the data out. It's not a good idea in general. A better idea is a scripted read-only field if you are using JIRA Server (and ScriptRunner)

April Jun 20, 2018

Matt, you are totally right in the context of active projects. Field data doesn't belong in summaries or descriptions.

However, we have a number of 'retired' (read-only) projects, several of which were the sole users of certain custom fields. 

As our Jira system grows, I want to also 'retire' (delete) old fields along with the old projects, but the humans have a great fear of losing data, even on issues created by Moses from before he left Egypt. 

Copying that data to the issue summary or description is a super-easy way of relieving any concerns the humans may have that their ancient entry in the vitally important 'field X' will be preserved for future generations, while allowing me to keep the current custom field count down to a level pleasing to Jira.

Thus, even without a real project archive option, everyone can still be happy, thanks to the miracle of ScriptRunner ;)

0 votes
Indranil Mondal Jun 22, 2016

clients want to get idea about the issue directly from its title without bothering to read individual fields...

0 votes
Stephen Letch Jan 14, 2019

Hi

 

I've tried the code

package UpdateIssue
 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
 
issue.setDescription(customFieldManager.getCustomFieldObjectByName("category").getValue(issue).toString() + "/" + customFieldManager.getCustomFieldObjectByName("name").getValue(issue).toString()

 

However I want to add to the description, not replace it, can you let me know what I need to add to update instead of replace? Also I'd like to copy the name of the custom field ahead of the contents of the custom field

 

Example

-----------------

Original Text in the description

 

Name of Custom field 1:

Contents of custom field 1

 

Name of custom field 2:

Contents of custom field 2

------------------

and so on.

Vasiliy Zverev Jan 15, 2019

You could try this code. I did not tested it, but should work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

StringBuilder descriptionBuilder = new StringBuilder();
descriptionBuilder.append(issue.getDescription())

for(String cstFldName: {"category"; "name"}){
descriptionBuilder.append("\n").append(cstFldName).append("\n").append(customFieldManager.getCustomFieldObjectByName(cstFldName).getValue(issue).toString())
}

issue.setDescription(descriptionBuilder.toString())
Stephen Letch Jan 15, 2019 • edited

Hi. This is the script that I managed to put together using various example scripts and researching markup for the formatting

 

package UpdateIssue  import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.fields.renderer.wiki.AtlassianWikiRenderer def description = issue.descriptionCustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();  issue.setDescription description + "\r\n"\  + 'h2.'+"Impact Details"\  + "\r\n"\  + "*Affected Users*"\  + "\r\n"\  + (customFieldManager.getCustomFieldObjectByName("Affected Users").getValue(issue).toString()\  + "\r\n"\  + "*Implemenation Details*"\  + "\r\n"\  + customFieldManager.getCustomFieldObjectByName("Implementation Details").getValue(issue).toString()\  + "\r\n"\  + "*Testing Details*"\  + "\r\n"  + customFieldManager.getCustomFieldObjectByName("Testing Details").getValue(issue).toString()  + "\r\n"\  + "*Rollback Steps*"\  + "\r\n"  + customFieldManager.getCustomFieldObjectByName("Rollback Steps").getValue(issue).toString()      + "\r\n"\  + "*Risk Mitigation Plan*"\  + "\r\n"  + customFieldManager.getCustomFieldObjectByName("Risk Mitigation Plan").getValue(issue).toString())

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in United States

Topic Tuesday: Agile

Another week, another Topic Tuesday!  Agile has been on my mind a lot lately because I've seen good implementations and bad implementations, but now as a Jira admin it matters more to me whether...

21 views 0 0
View post

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you