Forums

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

Copy Group Picker value to Sub-Task

Jan Mostowski
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!
May 22, 2020

Hi!

I have a problem - trying to copy value of Group picker to Sub-Task via behaviour.

But it's not working :(

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)
//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 = ['Grupa obsługująca']

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)
}
}

 

 

1 answer

0 votes
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 Leaders.
May 22, 2020

Can you please expand on what/why it doesn't work?

What errors or debug statements do you see in the log?

Which field is this script added to in the behaviour configuration?

If you look at this page in the documentation you will see a warning that issueParentId is not available in the initializer for new sub-tasks:

 As such, you will need to associate the script with a visible field. It will not work as an initialiser

https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/subtask-default-fields.html

Jan Mostowski
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!
May 25, 2020

Hi!

The problem is because nothing heppens - I don't see anything in logs related to this action from Behaviour.

Yes, I found this info that I have to associate this script to field another than initializer so I associated it to Summary field.

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 Leaders.
May 25, 2020

When things aren't working, it helps to add some debug messages, try this and see where the debug messages lead you (I removed commented-out lines):

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
log.setLevel(org.apache.log4j.Level.DEBUG)
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
log.debug "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()


OutlookDate outlookDate = ComponentAccessor.getComponent(OutlookDateManager).getOutlookDate(Locale.getDefault())
List copyCustomFields = ['Grupa obsługująca']

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

parentFields.each { cf ->
if (copyCustomFields && !copyCustomFields.contains(cf.name)) {
log.debug "$cf.name not found in $copyCustomFields"
return
}

def parentValue = cf.getValue(parentIssue)
if (!parentValue) {
log.debug "$cf.name doesn't have a value in $parentIssue.key"
return
}
getFieldById(cf.id).setFormValue(parentValue)
log.debug("parentValue: ${parentValue?.class} for type ${cf.name}")

if (parentValue instanceof Timestamp) {
log.debug "Timestamp value detected in $cf.name. Applying
${outlookDate.formatDMY(parentValue)}"
getFieldById(cf.id).setFormValue(outlookDate.formatDMY(parentValue))
} else if (parentValue instanceof Option) {
log.debug "Option value detected in $cf.name. Applying $parentValue.optionId : $parentValue.value"
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
log.debug "Multiple Options value detect in $cf.name. Applying ${parentValue.collect { it.optionId }}"
getFieldById(cf.id).setFormValue(parentValue.collect { it.optionId })
} else {
log.debug "other data type detected (${parentValue.getClass()}). Applying $parentValue as is"
getFieldById(cf.id).setFormValue(parentValue)
}
}

Suggest an answer

Log in or Sign up to answer