Custom Fields not Passing from Issue to Sub Task

Steve Turner July 2, 2019

Greetings -

I am having an issue creating a script in Behaviours that allows me to pass the pulldown list selected item to my new task I create under the same issue. See illustration below.  Custom Field name is Accounting Category (its default is TBD) - I want to pass the Operating Expense choice.

task.PNG  

Here is the screen shot of the behaviour set up.

beh.PNG

Here is the full script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

def accountingCategory = getFieldById(getFieldChanged())
def accountingCategoryValue = accountingCategory.value

if (!accountingCategoryValue){
return
}

def accountingCategoryIssueKey = accountingCategoryValue.toString().tokenize(":").last()
def accountingCategoryIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(accountingCategoryIssueKey)
def selectListCustomField = ComponentAccessor.customFieldManager.getCustomFieldObjects(accountingCategoryIssue).findByName("Accounting Category")
// could not find custom field with name "Accounting Category" on the accounting issue - do nothing
if (!selectListCustomField) {
return
}

def selectListValue = (accountingCategoryIssue.getCustomFieldValue(selectListCustomField) as LazyLoadedOption)?.value
def optionId = ComponentAccessor.optionsManager.getOptions(selectListCustomField.getRelevantConfig(accountingCategoryIssue)).find { it.value == selectListValue}?.optionId
getFieldByName("Accounting Category").setFormValue(optionId)

 

any help would be greatly appreciated.

2 answers

0 votes
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 9, 2019

Hello Steve,

Were you able to solve your issue following Adaptavist recommendations? 

You can use the post function in this article as a template for your need:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.fields.FieldManager

// the name of the field whose value we want to copy from parent to subtask
final String fieldNameToCopy = "Component/s"

FieldManager fieldManager = ComponentAccessor.fieldManager

if (!issue.isSubTask()) {
return
}

def fieldToCopy = fieldManager.allAvailableNavigableFields.find { it.name == fieldNameToCopy }
if (!fieldToCopy) {
log.info "Could not find field with name $fieldNameToCopy"
return
}

def parentIssue = issue.parentObject
def fieldToCopyId = fieldToCopy.id

switch (fieldToCopyId) {
case fieldManager.&isCustomFieldId:
def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(fieldToCopyId)
def linkedIssueCustomFieldValue = parentIssue.getCustomFieldValue(customField)
issue.setCustomFieldValue(customField, linkedIssueCustomFieldValue)
break

case IssueFieldConstants.COMPONENTS:
issue.setComponent(parentIssue.components)
break

case IssueFieldConstants.FIX_FOR_VERSIONS:
issue.setFixVersions(parentIssue.fixVersions)
break

case IssueFieldConstants.AFFECTED_VERSIONS:
issue.setAffectedVersions(parentIssue.affectedVersions)
break

default:
issue[fieldToCopyId] = parentIssue[fieldToCopyId]
}

Let me know if this information helps.

0 votes
Krishnanand Nayak
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.
July 2, 2019

how are you triggering the new task creation?

Steve Turner July 2, 2019

Either the pulldown create new sub-task or click the plus sign in the issue.

Krishnanand Nayak
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.
July 2, 2019

instead of behavior you should try using a Issue Create/Update Listener to copy the value from the Parent to the Sub-Task.

Behaviors are loaded on the current form, so might have to write additional code to get the issue context where the creation is triggered.

Steve Turner July 2, 2019

I am new in customization Jira with the adaptavist plugins.  So I need to do a listener instead of behaviours?  

Krishnanand Nayak
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.
July 2, 2019

here is how it will look

listner.png

also you get get some solution here : https://library.adaptavist.com/entity/create-a-sub-task-when-parent-issue-has-a-specific-label

Steve Turner July 5, 2019

After talking with Adaptavist - they told me to use a behaviour instead of a listener - so Not sure how to proceed here.

Krishnanand Nayak
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.
July 7, 2019

They are the experts. I would follow their inputs and they should have script samples too.

Suggest an answer

Log in or Sign up to answer