Copy Custom field values from Epic to Story

Utkarsh Agarwal November 8, 2017

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 @adammarkham 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.? 

4 answers

1 accepted

1 vote
Answer accepted
Utkarsh Agarwal November 21, 2017

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)
}
Sara December 15, 2017

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

Jimmy Seddon
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 8, 2019

Yes! Thank you @Utkarsh Agarwal! This is exactly what I needed as a starting point to resolve the my issue as well!

1 vote
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 10, 2017

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?

Utkarsh Agarwal November 10, 2017

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.
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 10, 2017

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.

Utkarsh Agarwal November 13, 2017

Thanks @adammarkham.

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: 

Utkarsh Agarwal November 13, 2017

Create.JPG

Utkarsh Agarwal November 13, 2017

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)

}
0 votes
sai praveen aminigadda November 19, 2019

why "MutableIssue issue = issue"  showing the error to me. why?

0 votes
Praveen November 10, 2017

Hi Utkarsh,

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

Utkarsh Agarwal November 10, 2017

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 Sign up to answer