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

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

This widget could not be displayed.

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

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.

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.

This widget could not be displayed.

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?

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

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);

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

292 views 5 0
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