Hi,
I am trying to have a custom text field called "Initiative" that exists in all issue types populated from Story to sub-task using scriptrunner. I am new to scriptrunner and only have admin access to a dev server for exploration so may be unfamiliar with admin settings needed to make this work.
Reference: https://scriptrunner.adaptavist.com/5.4.47/jira/recipes/behaviours/subtask-default-fields.html
Problems:
- I do not know how to do this: "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." Parent IssueID is not a field I can add to the form. - Should the script below work for text fields? I have ready that I may need to replace "option" with "string" but am uncertain where to do this.
Script (I removed some error-causing items that I don't think I need and retained some standard fields to inherit values from parent):
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.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 = ['Initiative']
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)
}
}