Using a behaviour to copy a multi select custom field from an Epic

Louis Drolet April 22, 2017

When creating an issue and adding it to an Epic by setting the Epic link, we wish to copy a series of custom fields from the Epic to the new issue, since they should have the same values.  I have succeeded in copying single-select custom fields (such as Theme in my example), but I cannot get the copy of multi-select custom fields to work (the Product field in my example).

Here is the code in my ScriptRunner Behaviour :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def theme = getFieldByName("Theme")
def product = getFieldByName("Product")

def epicLink = getFieldByName("Epic Link")
String epicLinkKey = epicLink.getValue()
String epicLinkNoKey = epicLinkKey.replaceAll("key:", "")
def epic = ComponentAccessor.getIssueManager().getIssueObject(epicLinkNoKey)

def cfTheme = customFieldManager.getCustomFieldObjectByName("Theme")
def epicTheme = epic.getCustomFieldValue(cfTheme) as String
def configTheme = cfTheme.getRelevantConfig(getIssueContext())
def optionsTheme = optionsManager.getOptions( configTheme )
def optionToSelectTheme = optionsTheme.find { it.value == epicTheme }
theme.setFormValue(optionToSelectTheme.optionId)

def cfProduct = customFieldManager.getCustomFieldObjectByName("Product")
def epicProduct = epic.getCustomFieldValue(cfProduct)
def configProduct = cfProduct.getRelevantConfig(getIssueContext())
def optionsProduct = optionsManager.getOptions( configProduct )
def optionToSelectProduct = optionsProduct.findAll { it.value in epicProduct }
product.setFormValue(optionToSelectProduct*.optionId)

I also have a single User selector that I wish to copy from the Epic. Can someone help ?

2 answers

0 votes
Sushil Ranjan March 29, 2022

Hi, I have same scenario like Louis but my requirement is for "Text field-Single Line". Please suggest what would be the solution for the 'text' field? Thanks!

0 votes
adammarkham
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.
April 25, 2017

The problem is the custom field value you get from the JIRA API is not always the same as the one you set using behaviours.

If you always want the same fields copied without any edits allowed by the user I'd suggest using a custom scripted post-function on the create issue transition.

You should be able to just copy the fields over you want like so:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue = issue as MutableIssue

// add more fields to this list that you want to copy:
["Product", "Theme"].each {
    def cf = customFieldManager.getCustomFieldObjectByName(it)

    def value = epic.getCustomFieldValue(cf)

    issue.setCustomFieldValue(cf, value)
}

You'll need to add more fields to the list.

Please see here for the position the post-function needs to be placed in. Just before the Update change history for an issue and store the issue in the database step.

Hope this helps.

Louis Drolet April 25, 2017

Thanks for the quick response Adam.

What I like about the behaviour solution is that as soon as a user sets the epic link in the creation screen, the other fields populate instantly, and the user still has the possibility to change the value of the fields for the exceptional case where they should have a different value (it happens).

What I understand is that the value returned by the JIRA API does not have the correct "format" for the behaviour function setFormValue. Is it simply a matter of string formatting that can be handled in the sccript ? I noticed that for a username, the format returned is "username (username)", which could be fixed by stripping the end of the string. But for the multi-select list, I am not quite sure if it can be reformatted.

What do you think ?

adammarkham
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.
April 26, 2017

Thanks for explaining your use case a bit more. Yes behaviours are a good fit for this.

For the user picker it should be a case of doing something like:

userPickerField.setFormValue("admin")

For a multiselect the example in the documentation here should work for you. It's the second one in that example.

There's also a bunch of examples in the documentation for setting other field types.

Let us know how you get on with that.

Utkarsh Agarwal November 8, 2017

Hi @adammarkham I am trying to use the post function script you provided:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue = issue as MutableIssue

// add more fields to this list that you want to copy:
["Product", "Theme"].each {
    def cf = customFieldManager.getCustomFieldObjectByName(it)

    def value = epic.getCustomFieldValue(cf)

    issue.setCustomFieldValue(cf, value)
}

However, i am getting error on epic, am i missing something here?
The variable is undeclared, I tried adding:

def epic = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}  

However, this does not seems to work.  Kindly let me know if i am missing something. 

My requirement is to simply copy the field values from epic to story during creation.

 

Thanks

Utkarsh

adammarkham
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.
November 8, 2017

@Utkarsh Agarwal - can you please start a new question, with the full script your using. That way it will be seen by more people and I'll answer it.

Suggest an answer

Log in or Sign up to answer