Scriptrunner post function: Update a custom text field with values from other custom fields

I have installed Scriptrunner to resolve the following issue:

In my JIRA core instance I have created a workflow. When moving an issue from "open" to the next step in the workflow I have defined a screen. In that screen, the user has to enter values to a number of custom text fields. When I close the screen I would like to run a post function that calculates the concatenation (with a space as separator) of two separate custom text fields into a third custom text field.

An example:

Custom Text Field 1 value: "abcd"

Custom Text field 2 value: "efgh"

"calculated" Customer Text field 3 value: "abcd efgh"

In this example, the user has provided values for Customer Text fields 1 and 2 in the screen but not for Customer Text field 3.

Although I have some experience with coding (mostly in the R domain, so mostly functional programming) I am a newby to JIRA and Scriptrunner. I have looked at various examples but for instance fail to understand how to concatenate the fields and update the DB.

And would such code also work if the 'calculated' field would be the summary field?

Any help, or sample scripts, would be very helpful.

I am using JIRA Core v7.2.1 and Scriptrunner 4.3.11

Many thanks!

2 answers

1 accepted

Into script postfunction you have issue varialbe which represents curretn issue. Namely an instance of MutableIssue class.

There are two types of issue values: in-build one and custome. There are special method for in-build values into MutableIssue class and the onle one for custom field: setCustomFieldValue.

If you have deal with create transition you should cal IssueManager.updateIssue() method to store changes into a DB. In other cases place your function before in-build post-function that stores issue into a DB.

Here is code for your question:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

issue.setCustomFieldValue(
        customFieldManager.getCustomFieldObjectByName("Text field 3")
        , customFieldManager.getCustomFieldObjectByName("Text field 1")
        + customFieldManager.getCustomFieldObjectByName("Text field 2")
)

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        ,issue
        , EventDispatchOption.ISSUE_UPDATED
        , false
)

Thanks much Vasiliy!

I created a test custom field called "RMA scripted test field" and copied your code in the "Script fields" page of the Script-runner add-in. I get several error messages. Apologies to ask for your help once more.

2016-10-24 16_56_34-Script Fields - JIRA Manufacturing - WSS.png

and:

2016-10-24 16_57_39-Script Fields - JIRA Manufacturing - WSS.png

Paul

Hm, here is for JIRA 7:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
 
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
 
issue.setCustomFieldValue(
        customFieldManager.getCustomFieldObjectByName("Text field 3")
        , customFieldManager.getCustomFieldObjectByName("Text field 1")
        + customFieldManager.getCustomFieldObjectByName("Text field 2")
)
 
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        ,issue
        , EventDispatchOption.ISSUE_UPDATED
        , false
)
1 votes
Mahesh S Community Champion Oct 23, 2016

Hi Paul,

This works for me!!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField field1 = customFieldManager.getCustomFieldObjectByName("Custom field 1 Name")
CustomField field2 = customFieldManager.getCustomFieldObjectByName("Custom field 2 Name")
String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = (String)issue.getCustomFieldValue(field2)
def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 3 Name"}
def changeHolder = new DefaultIssueChangeHolder();
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value+" "+field2Value),changeHolder);

Cheers!!

Hi Mahesh,

With a small adjustment, realizing that the the "Custom field 1" and "Custom field 2" needed to be derived from the current issue, I got the following to work correctly:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 1"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 2"}
String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = (String)issue.getCustomFieldValue(field2)
def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 3"}
def changeHolder = new DefaultIssueChangeHolder();
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + " " + field2Value),changeHolder);

This code is positioned as a post function in one of the transitions in my work flow.

Thanks for pointing me in the right direction.

Paul

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

357 views 1 13
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot