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)
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.
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.
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);
Hey admins! I’m Dave, Principal Product Manager here at Atlassian working on our cloud platform and security products. Cloud security is a moving target. As you adopt more products, employees consta...
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!
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