Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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 default the value of a custom field on a sub-task from the parent using ScriptRunner

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

Valerie,

Have you made sure that this script is NOT being used in an initializer of the behaviour? You need to attach this script to a field, e.g. description. From our docs: "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."

Regards,

Josh

Correct.  The script is attached to a field on the screen, not used as an initializer.  I was able to get non-custom fields like assignee to work, but was getting inconsistent results for the custom field unfortunately.   I ended up finding another solution since I couldn't get this to work. 

Hi Valerie,

I'm sorry to hear that!  If you're still interested in trying to make it work, I have some questions for you that might help me try to think of a fix.

You mentioned you want to set a custom field that is a drop-down. Is this a standard custom field, as in the standard Select List custom field in Jira? Or does this drop-down field come from another plugin?

Which version of ScriptRunner are you on? I see you're trying to use one of the examples in our documentation here: https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/subtask-default-fields.html

Your code has a logging statement 

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

You might look in the atlassian-jira.log and see what's being logged. That might give us some clues as to what's going wrong.

Regards,

Josh

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

101 views 0 6
Read article

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