Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Inherit Multi-user Picker Values from Parent in Sub-Task Create Form

Features
Behaviours
Created 1 year ago, Updated 27 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
// Set log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

// Define the user picker field param
final multiUserPickerFieldName = 'Stakeholders'

// Get components
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def changedFormField = getFieldById(fieldChanged)
def parentIssueFormField = getFieldById('parentIssueId')
def parentIssueId = parentIssueFormField.formValue as Long

log.debug("The parent issue id: ${parentIssueId}")
log.debug("The changed field form value: ${changedFormField.formValue}")
// Do nothing if the issue is not a subtask or the field associated with the script already has data
if (!parentIssueId || changedFormField.formValue) {
    return
}

// Get the parent issue
def parentIssue = issueManager.getIssueObject(parentIssueId)
// Get the value of the user picker in the parent issue
def parentIssueUserPickerCustomField = customFieldManager.getCustomFieldObjects(parentIssue)?.find { it.name == multiUserPickerFieldName }
def parentIssueUserPickerCustomFieldValue = (parentIssueUserPickerCustomField ? parentIssue.getCustomFieldValue(parentIssueUserPickerCustomField) : null) as List<ApplicationUser>

// Do nothing if the field doesn't have a value in the parent issue or it's not really an issue picker
if (!parentIssueUserPickerCustomFieldValue) {
    return
}

log.debug("The parent user picker values: ${parentIssueUserPickerCustomFieldValue*.username}")
def userPickerFormField = getFieldByName(multiUserPickerFieldName)
userPickerFormField.setFormValue(parentIssueUserPickerCustomFieldValue*.username)
Having an issue with this script?
Report it here