Copy Custom field values from Epic to Story

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

I am using a custom scripted post-function on the create issue transition.

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

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


["Vendor Name", "Location", "Group", "GCRS Sector", "Impacted Regions"].each {
def cf = customFieldManager.getCustomFieldObjectByName(it)
def epic = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}
def value = epic.getCustomFieldValue(cf)

issue.setCustomFieldValue(cf, value)
}

as per the answers by @Adam Markham [Adaptavist] here: https://community.atlassian.com/t5/Jira-questions/Using-a-behaviour-to-copy-a-multi-select-custom-field-from-an/qaq-p/579955#U668298 

 I am getting error at "cannot find matching method getCustomFieldValue"

def value = epic.getCustomFieldValue(cf)

Being new to scripts can you please point me towards some relevant document or example to get this resolved.? 

3 answers

1 accepted

Finally got the script working:

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

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


["Vendor Name", "Location", "Group", "GCRS Sector", "Impacted Regions"].each { cfname ->
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == cfname}
def epicCf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}
if (!cf || !epicCf) {return}

def epic = issue.getCustomFieldValue(epicCf) as Issue
if (!epic) {return}

def cfValue = cf.getValue(epic)

issue.setCustomFieldValue(cf, cfValue)
}

Thanks Utkarsh. Your code helped me a lot. Actually i have a request to copy "Capitalizable?" from Epic to Story when story transitioning from Merged  to Closes. Its working.

 

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

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


["Capitalizable?"].each { cfname ->
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == cfname}
def epicCf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}
if (!cf || !epicCf) {return}

def epic = issue.getCustomFieldValue(epicCf) as Issue
if (!epic) {return}

def cfValue = cf.getValue(epic)

issue.setCustomFieldValue(cf, cfValue)
}

1 votes

What is contained in the "epic" variable? It should contain the epic as an Issue object.

Can you try adding the following lines below the one where we get the epic

 log.warn "Epic: $epic"
log.warn "Custom fields ${customFieldManager.getCustomFieldObjects(issue)*.name}"

What does that output in the logs?

Hi Adam,

These logs returns:
Epic: Epic Link
Custom fields [Acceptance Criteria, Actual Time in Status, Add Comments, Additional Action, Additional Candidates, Additional Details, Adoption Status, Aggregate Story Points, Analysis Required, ...............................................]

The fields required are present in above logs. So this clears the doubt about connection with epic.

I guess the below method is not picking values:

epic.getCustomFieldValue(cf)

I am getting:

[Static type checking] - cannot find matching method
please check if the declared type is right or if the method exists.

Apologies, that script is slightly wrong. The example below should work for you:

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

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


["Vendor Name", "Location", "Group", "GCRS Sector", "Impacted Regions"].each {
def cf = customFieldManager.getCustomFieldObjectByName(it)
def epicCf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}


def epic = epicCf.getValue(issue) as Issue

def cfValue = cf.getValue(epic)

issue.setCustomFieldValue(cf, cfValue)
}

 First we need to get the epic as an Issue object from the epic-link custom field. Then we can copy the values over from the epic.

Hope this helps.

Thanks @Adam Markham [Adaptavist].

This really does fetch the values from the epic custom fields. However the values are not being set for the targeted story.

The fields i am trying to populate are of select list types. I tried to modified your code:

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

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


["Vendor Name", "Location", "Group", "GCRS Sector", "Impacted Regions"].each {
def cf = customFieldManager.getCustomFieldObjectByName(it)
def epicCf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}


def epic = epicCf.getValue(issue) as Issue

def cfValue = cf.getValue(epic)

def cfConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {cfValue.toString()}

issue.setCustomFieldValue(cf, value)
log.error "Value: $value"
}

Still no luck. The post function is set at appropriate position: 

I Believe there is some issue to Set a select list custom field value

Even the below code does no seem to work:

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

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


["Vendor Name"].each {
def cf = customFieldManager.getCustomFieldObjectByName(it)

def cfConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Wipro'}

issue.setCustomFieldValue(cf, value)

}

Hi Utkarsh,

Please try, "def value = issue.getCustomFieldValue(cf)" it should work now.

Hi Praveen,

I tried this, however this picks the value from the story and maps it back to story.

My requirement is to have the custom field value taken from epic and mapped to story during issue creation.

Probably need to setup a connection between epic and story before mapping the values from epic to story.

Note: Fields context and custom fields are already present on screens for both epic and story.

Thanks
Utkarsh

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,990 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot