Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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)

 

 

 

 

 

 

 

1 answer

1 vote

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
}

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?

Any possibilities to copy attachments too ?

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)

@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.

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()}"

@Peter-Dave Sheehan 

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

Thanks,

Siva.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you