How to copy field from parent to subtask on create screen?

Claudie
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 18, 2020

Hi, 

I'd like to copy fields from parent to subtask on create screen.

Whenever I create subtask, I want to get the field value from parent.

I've tried to use both listener and script runner, but I've failed. 

What more should I do? What more settings should I make?

 

ref. https://scriptrunner.adaptavist.com/5.4.47/jira/recipes/behaviours/subtask-default-fields.html

 

I added below script on 'Behaviour Settings > Initialiser' but it's not working.

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.web.util.OutlookDate
import com.atlassian.jira.web.util.OutlookDateManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

import java.sql.Timestamp

import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours

FormField field = getFieldById(getFieldChanged())
FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long


if (!parentIssueId || field.getFormValue()) {
// this is not a subtask, or the field already has data
return
}

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// REMOVE OR MODIFY THE SETTING OF THESE FIELDS AS NECESSARY
getFieldById(SUMMARY).setFormValue(parentIssue.summary)
getFieldById(PRIORITY).setFormValue(parentIssue.getPriority().id)

//OutlookDate outlookDate = ComponentAccessor.getComponent(OutlookDateManager).getOutlookDate(Locale.getDefault())
//getFieldById(DUE_DATE).setFormValue(outlookDate.formatDMY(parentIssue.getDueDate()))

//getFieldById(COMPONENTS).setFormValue(parentIssue.componentObjects*.id)
//getFieldById(AFFECTED_VERSIONS).setFormValue(parentIssue.affectedVersions*.id)
//getFieldById(FIX_FOR_VERSIONS).setFormValue(parentIssue.fixVersions*.id)
//getFieldById(ASSIGNEE).setFormValue(parentIssue.assigneeId)
//getFieldById(ENVIRONMENT).setFormValue(parentIssue.environment)
getFieldById(DESCRIPTION).setFormValue(parentIssue.description)
//getFieldById(SECURITY).setFormValue(parentIssue.securityLevelId)
//getFieldById(LABELS).setFormValue(parentIssue.labels)

 

 

 

 

 

 

 

2 answers

1 vote
Peter-Dave Sheehan
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.
May 23, 2020

Behaviour is definitely the direction you want to go if you want those fields pre-populated in the create screen.

But you missed a critical alert in the documentation page you linked:

This script hinges on the parentIssueId being present in the form. As such, you will need to associate the script with a visible field. It will not work as an initialiser.

So try to associate the script with the summary field for example.

Note that if you edit the sub-task after it's been created and then clear the summary field, then all the defaults from the parent will be re-applied.

You can fix this by changing line 21-24 to:

if (!parentIssueId || underlyingIssue) {
    // this is not a subtask, or the issue has already been created and we don't need default values
    return
}
tobias November 26, 2020

Hello,
The script works great. Thanks for this. I wonder if it's possible to get the script to copy the attachments to the subtask as well?

Lokesh
Contributor
October 17, 2021

Any possibilities to copy attachments too ?

Peter-Dave Sheehan
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.
October 18, 2021

Probably not from a behaviour implementation since the interactions are mostly browser-side or client-side. There are samples out there for how to copy attachments from server side implementations (e.g. in a workflow post-function)

siva
Contributor
January 17, 2022

@Peter-Dave Sheehan 

I saw your comment on this post and my issue is related to this and i used below script and it working perfectly coping summary field from parent to sub:task but here i need to add default value before summary (Tested:) and copied the summary from parent please help me with where to add script in the below script 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.web.util.OutlookDate
import com.atlassian.jira.web.util.OutlookDateManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

import java.sql.Timestamp

import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours

FormField field = getFieldById(getFieldChanged())
FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long


if (!parentIssueId || field.getFormValue()) {
// this is not a subtask, or the field already has data
return
}

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// REMOVE OR MODIFY THE SETTING OF THESE FIELDS AS NECESSARY
getFieldById(SUMMARY).setFormValue(parentIssue.summary)

 

 

 

 

Thanks in advance,

Siva.

Peter-Dave Sheehan
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.
January 18, 2022

That's quite simple... just use groovy string interpolation:

def newSummary = "Tested: $parentIssue.summary"
getFieldById(SUMMARY).setFormValue(newSummary)

Any string delimited by double quotes can include variables or object values defined from elsewhere in the script. Just prefix it with a $.

If the expression you want to return is more complicated than just single words separated by dots, you have to surround that expression with {}.

E.g

def newSummary = "Tested: ${parentIssue.summary.toUpperCase()}"
siva
Contributor
January 18, 2022

@Peter-Dave Sheehan 

Thanks for the reply it is working once again thank you very much and very appreciated.

Thanks,

Siva.

0 votes
Robert Bromley October 11, 2023

How do you copy a custom field over?

Suggest an answer

Log in or Sign up to answer