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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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?

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