Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to default the value of a custom field on a sub-task from the parent using ScriptRunner

Valerie Rouda
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 27, 2018

I'm using ScriptRunner to default fields from the parent when a sub-task is created.  I have a custom field that is a dropdown and I'm having trouble getting this field on the sub-task to default to the value from the parent.  I'm used the sample code from Script Runner, making just a few modifications for the fields I'm interested in:

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

OutlookDate outlookDate = ComponentAccessor.getComponent(OutlookDateManager).getOutlookDate(Locale.getDefault())

getFieldById(ASSIGNEE).setFormValue(parentIssue.assigneeId)

// 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 = ['customfield_100']
List<CustomField> parentFields = customFieldManager.getCustomFieldObjects(parentIssue)

for (def cf in parentFields) {
if (copyCustomFields && !copyCustomFields.contains(cf.name)) {
return }

def parentValue = cf.getValue(parentIssue) as List<Option>
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 && parentValue[0] instanceof Option) {
getFieldById(cf.id).setFormValue(parentValue*.optionId)
}
else { getFieldById(cf.id).setFormValue(parentValue)
}
}

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
October 26, 2022

Behaviours are evaluated every time a screen dialog is open. That's true for Create screen, Edit screen or any transition screens.

Workflow Validators fire when you submit a transition form (or immediately if no screens are associated with the transition).

It's possible to use getActionName() in your behaviour to see if you are using a transition or not and perhaps bypass the validation then.

DEPLOYMENT TYPE
SERVER
VERSION
8.22.6
TAGS
AUG Leaders

Atlassian Community Events