Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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

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 ?

1 answer

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.

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 ?

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.

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

@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
TAGS

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