Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Copy Custom Fields from Subtask to Parent Issue (w/ ScriptRunner)

Christopher Gronde February 9, 2017

So I discovered that JIRA can only copy a field from parent to subtask and not the other way around without yet ANOTHER paid plugin.

We have script runner and I am trying to find a way to script out a post function that will copy a field from a subtask into the same field in the parent ticket.  There are several subtasks that collect data and I want to copy each other those from their respective subtask into one place (i.e. the parent ticket).  Below is what I have so far but the post function doesn't even run.  Not sure what to do.

 

import com.atlassian.jira.component.COmponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
 
String copyFrom="customfield_13467"
String copyTO="customfield_13467"
 
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userUtil = ComponentAccessor.getUserUtil()
def parentIssue = (MutableIssue)issue.getParentObject()
def IssueManager = ComponentAccessor.getIssueManager();
def jac = ComponentAccessor.getJiraAuthernicationContext();
 
def cfObj = customFieldManager.getCustomFieldObject(copyFrom)
def valcfObj = issue.getCustomFieldValue(cfObj)
def cf2Update = customFieldManager.getCustomFieldObject(copyTo)
def valcf2Update = parentIssue.getCustomFieldValue(cf2Update)
 
 
if (valcf2Update == null){
	parentIssue.setCustomFieldValue(cf2Update,valcfObj)
	} 
 
IssueManager.updateIssue(jac.getLoggedINUser(), parentIssue, EventDispatchOption.ISSUE_UPDATED, false);

 

JIRA 7.1.2

ScriptRunner 4.3.16

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Christopher Gronde February 10, 2017

yeah that's because our stupid browser will not let me cut and paste code into these little blocks on here, so I have to type everything out line by line

0 votes
Vasiliy Zverev
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.
February 9, 2017

Here is refactored version of your code compatible with JIRA 7. I would reccomed to use an IDE for code writing. I use Intellj IDEA.

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
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.fields.CustomField

MutableIssue parentIssue = issue.getParentObject()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField copyFrom = customFieldManager.getCustomFieldObject("customfield_13467")
CustomField cf2Update = customFieldManager.getCustomFieldObject("customfield_13467")
def valcf2Update = parentIssue.getCustomFieldValue(cf2Update)

if (valcf2Update == null){
    parentIssue.setCustomFieldValue(cf2Update, issue.getCustomFieldValue(copyFrom))
}

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , parentIssue
        ,  UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()  );
Judah October 24, 2018

@Vasiliy Zverev

I am trying to apply this code as a listener on the event 'issue created'...I want to copy down the value of my custom field (select list - single choice) from parent to child on creation. 

Currently I get the error (issue variable is undeclared) on the line: 

MutableIssue parentIssue = issue.getParentObject()

 Any insight? 

0 votes
Robert Dzido
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.
February 9, 2017

First of all change COmponentAccessor to ComponentAccessor

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events