Sub-task Default Field

This example shows you how to use default fields on the Create action for sub-tasks, derived from the parent issue fields.

The key to this is getting the parent issue ID via the parentIssueId form field. Once you have that you can load that issue, get the fields, and set them on the Create Issue dialog.

The sample code demonstrates copying all fields from the parent to the new sub-task. In practice you will want to select just some system or custom fields, by editing this example.

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.

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.AFFECTED_VERSIONS
import static com.atlassian.jira.issue.IssueFieldConstants.ASSIGNEE
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
import static com.atlassian.jira.issue.IssueFieldConstants.DESCRIPTION
import static com.atlassian.jira.issue.IssueFieldConstants.DUE_DATE
import static com.atlassian.jira.issue.IssueFieldConstants.ENVIRONMENT
import static com.atlassian.jira.issue.IssueFieldConstants.FIX_FOR_VERSIONS
import static com.atlassian.jira.issue.IssueFieldConstants.LABELS
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY
import static com.atlassian.jira.issue.IssueFieldConstants.SECURITY
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY

@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.getPriorityObject().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)

// IF YOU DON'T WANT CUSTOM FIELDS COPIED REMOVE EVERYTHING BELOW HERE
// IF YOU ONLY WANT SOME FIELDS INHERITED ADD THEM TO THE LIST BELOW, OR LEAVE EMPTY FOR ALL
// eg ['Name of first custom field', 'Name of second custom field']
List copyCustomFields = []

List<CustomField> parentFields = customFieldManager.getCustomFieldObjects(parentIssue)

parentFields.each { cf ->
    if (copyCustomFields && !copyCustomFields.contains(cf.name)) {
        return
    }

    def parentValue = cf.getValue(parentIssue)

    if (!parentValue) {
        return
    }

    getFieldById(cf.id).setFormValue(parentValue)
    log.debug("parentValue: ${parentValue?.class} for type ${cf.name}")

    if (parentValue instanceof Timestamp) {
        getFieldById(cf.id).setFormValue(outlookDate.formatDMY(parentValue))
    } else if (parentValue instanceof Option) {
        getFieldById(cf.id).setFormValue(parentValue.optionId)
    } else if (parentValue instanceof List<Option>) {
        parentValue = parentValue as List<Option> //This cast is just to placate the static type checker
        getFieldById(cf.id).setFormValue(parentValue.collect { it.optionId })
    } else {
        getFieldById(cf.id).setFormValue(parentValue)
    }
}