how to append value updated in Single line text custom field to Multi line text custom field using Groovy script?

Hi,

Want to copy value updated in Single line text custom field (i,e Doc Version) to Multi line text custom field (i,e All Versions) using Groovy script?

//assuming this is a sigle line text field
def textFieldA = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Doc Version")
//assuming this is a Multi line text field
def textFieldB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("All Versions")
 
issue.setCustomFieldValue(textFieldB, textFieldA) ==> is it appends values???
The "Doc Version" custom field value will get updated by issue assignee multiple times in my workflow @ different workflow transition steps.
I want to save all the version updated by assignee instead of overwriting it, I am trying to copy values into another Multi line text custom field i,e All Versions for Team's future reference.
Is this the right way or using one Multie line text custom field itself, we can try to preserve the previous values and get appended with latest version values using only one field itself? Please advice.   
  • Manju 

2 answers

1 accepted

0 votes
Accepted answer

Got it, the below script works as per my requirement.

'\n' between prev and current field value adds new line between 2 values. It works. Thanks.

import com.atlassian.jira.issue.Issue 
import com.atlassian.jira.issue.fields.CustomField 
import com.atlassian.jira.component.ComponentAccessor
 
CustomField textFieldB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("SDD Document All Versions") 
 
issue.setCustomFieldValue(textFieldB, String.valueOf(issue.getCustomFieldValue(textFieldB)) + '\n' + String.valueOf(issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("SDD Baselined Doc Version"))))
  • Manju
1 vote
Vasiliy Zverev Community Champion Feb 26, 2017

Try this code

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

CustomField textFieldB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("All Versions")

issue.setCustomFieldValue(textFieldB, issue.getCustomFieldValue(textFieldB) + issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Doc Version")))

Thanks for your prompt response Vasiliy.

I am getting the below ERROR, when I tried to use the script you had suggested in Custom script post-function.

Static Type Checking - cannot find matching method

java.lang.Object#plus(java.lang.Object). Please check if declared type is right and if method exists

Possible solutions: use([Ljava.lang.Object;), is(java.lang.Object), use(java.lang.Class, groovy.lang.Closure), split(groovy.lang.Closure), wait(), find() @ line 8, column 39.

 

I am using JIRA Software 7.2.7 and SR version is 4.3.16

Hi Vasiliy,

Was trying alternate option of Using "Update Issue Custom Field" Add Post Function To Transition, updates an issue custom field to a given value. So I am trying to get the value from another custom field and trying to append it to the another 'Issue Custom Field' using this post function.

But I am unable to get the value of Custom Field Value from custom field id i,e customfield_10104

1) Issue Custom Field: SDD All Versions

2) Custom Field Value: customfield_10104 ==> this is another cusomt field id, from which I am trying to copy the value to 'SDD All Versions' custom field) here simply entering the ID of the option to be selected to get the value.

 

Append value:         If set, the given value will be appended to the field. Or if field supports multiple values, then the given value will be set without unsetting other ones.

Please make sure that the value you enter is valid for custom field its datatype and context configuration for the project using this workflow. Otherwise, the transition may fail at execution time. Additionally to enter text or numbers as value, you may use the following:

  • You may use macro '%%CURRENT_USER%%' to insert the function caller.
  • You may use macro '%%CURRENT_DATETIME%%' to insert the current date and time.
  • You may use macro '%%ADD_CURRENT_USER%%' to append the function caller. Obsolete please use append option instead with user macro above.
  • For Cascading Select fields, you may either use the value of the option to be selected, no need to add the parent for childs. Or simply enter the ID of the option to be selected.

 

  • Manju 
Vasiliy Zverev Community Champion Feb 27, 2017

Lets try this one:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

CustomField textFieldB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("All Versions")

issue.setCustomFieldValue(textFieldB, String.valueOf(issue.getCustomFieldValue(textFieldB)) + String.valueOf(issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Doc Version"))))

Thanks Vasiliy Zverev.

The shared script is able to append the values, but is it possible to copy values into "All Versions" from "Doc Version" into new line? As "All Versions" customfield is a Multi line Text.

So that reading values will be easier for JIRA Assignee's reference. Please advice how to do this in the shared script. Thanks.

Doc Version: 012345 (first time updated value)

Doc Version: 67890 (second time updated value)

 

The appended values started with "null" value. How to remove the "null" from the result

RPA-Cumu-Fulfilled-Res-Offshore:null[104294 - Bhandary,Venugopala, 104842 -                                                                       Shaikh,Anjumaara Mubarak]

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked Thursday in Jira Ops

I'm John Allspaw, Ask Me Anything about incident analysis and postmortems

I'm John Allspaw, co-founder of   Adaptive Capacity Labs, where we help teams use their incidents to learn and improve. We bring research-driven methods and approaches to drive effective inciden...

4,335 views 20 12
View question

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