Script Runner - Copy Custom Field Value from Parent When Sub-Task is Created

Jacob Clouse July 2, 2014

When Sub-Tasks are created from a Story, I would like the parent Story's custom field value to be copied to the Sub-Task's custom field value. For example: I have a field named "Client" and I want the value to appear within the Sub-Task's "Client" field after the Sub-Task is created.

I am not interested in a paid add-on for JIRA and would like to use Script Runner instead.

I have tried to test my script below using the console, but am getting errors related to "issue" and "transientVars" not being understood.

Thank you for your help in advance!

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
 
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()

CustomField customField_name = customFieldManager.getCustomFieldObjectByName('Client')

def parentMyFieldValue = transientVars["issue"].getCustomFieldValue(customField_name)
issue.setCustomFieldValue(customField_name, parentMyFieldValue)

2 answers

1 accepted

3 votes
Answer accepted
Vijay Khacharia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2014

Hi,

If you run the script in script console, you have to use an issue as an example. Its not there in the context when you run it from the console.

For testing purposes, use below line in the code.

issue = ComponentManager.getInstance().getIssueManager().getIssueObject("TEST-1234")

If you put the script in post function, you might not get error for this.

Option will also be to do use the JIRA Suite Utilities Plugin, its free.

https://marketplace.atlassian.com/plugins/com.googlecode.jira-suite-utilities

The plugin provides the post-function to copy value from other field, from parent to sub-task. You can add this post-function in the create transition for the sub-task.

Vijay

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 3, 2014

As Vijay says... if you test this as a workflow action it should work.

Note that in this scenario, issue refers to the new issue being created, and transientVars["issue"] refers to the parent or source issue.

Jacob Clouse July 3, 2014

Thank you! I decided to try out the JIRA Suite Utilities Plugin and it did just what I needed. I created a post function for each field that I wanted to copy from the parent using the "Copy Value From Other Field" function.

Alan October 30, 2019

JSU will not do this in Jira Cloud for the "Accounts" field from Tempo.  The JSU post-function does not include the ability to run the post-function as the transitioning user vs the addon-user.

Hoping that will get fixed soon!

Benjamin Weber -Beecom- October 31, 2019

Hi Alan

We're still working on getting all the features on Server to Cloud. However, as we have to basically re-create the App on a different platform, this is taking a while.

Stay tuned for updates!
Benjamin

0 votes
Paul Stallworth
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 16, 2016

Is this answer still correct for JIRA 7 API?  I had to change to use ComponentAccessor as there is no longer a getCustomFieldManager() in ComponentManager.  Also, this line statement throws an error 

def parentMyFieldValue = transientVars["issue"].getCustomFieldValue(cfParent)

could not find java.lang.Object#getCustomFieldValue.  Do I need to cast transientVars["issue"] to something more appropriate?

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2016

What is the context here, are you using one of the built-in scripts, or is this completely custom?

Yes, that API has changed, you can read what you need to do here: https://scriptrunner.adaptavist.com/latest/jira/releases/UpgradingToJira7.html

Paul Stallworth
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 18, 2016

Thanks for pointing me to the JIRA 7 page.

 

I initially tried to drop in the code from above as I have the same need as this question.  I got an error when trying to use transientVars so I poked around and came up with the code below.  It works but I'm not sure if it is the best way now so if you have any critiques I'll take them.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_10603')
def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)
def changeHolder = new DefaultIssueChangeHolder();
field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), parentMyFieldValue),changeHolder);
Like CITTI Dokunet likes this
Tukaram Bhukya June 7, 2017

Paul, do you have any idea  to apply above code for fix version copy ?

Suggest an answer

Log in or Sign up to answer