Copy values from Epic to Story on creation

CGM July 23, 2015

Hi,

we use the Behaviors plugin to copy values from story to subtask on creation.

This script is working well:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.option.Option
import com.opensymphony.workflow.loader.StepDescriptor
 
def ComponentManager componentManager = ComponentManager.getInstance(); 
def issueid = getFieldById("parentIssueId").value;
def Issue parentIssue = componentManager.getIssueManager().getIssueObject(Long.parseLong(issueid));
// Check workflow ID
if (Double.parseDouble(componentManager.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProject()).get("id").toString()) == 11600 ||
Double.parseDouble(componentManager.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProject()).get("id").toString()) == 11601)
{
// Set Cost Unit
def FormField cu = getFieldById(getFieldChanged());
def CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
def CustomField parentcustomField = customFieldManager.getCustomFieldObjectByName("Cost Unit");
def Option parentnrd = (Option)parentIssue.getCustomFieldValue(parentcustomField);
cu.setFormValue(parentnrd.getOptionId());
    
// Set FixVersions
def fixVersions = parentIssue.getFixVersions();
def FormField fv = getFieldById("fixVersions");
fv.setFormValue(fixVersions*.id);
    
// Set Components
def components = parentIssue.getComponentObjects();
def FormField c = getFieldById("components");
c.setFormValue(components*.id); 
    
}

 

Now we try the same when creating a story from an epic using the "Issues in Epic" "+" function but with no success. The information about the parent epic must be available because in the headline of the story create dialog we can see "Create issue in epic: TEST-1".

Does anyone know what we are doing wrong and how we can solve this problem?

Best regards,

Jo

5 answers

1 accepted

0 votes
Answer accepted
CGM August 26, 2015

Finally we manged to get the information from the Epic.

This is the script we use:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.option.Option
def ComponentManager componentManager = ComponentManager.getInstance(); 
def FormField field = getFieldByName("Epic Link");
if (field != null)
{
def Issue parentIssue = componentManager.getIssueManager().getIssueObject(field.getValue().toString().substring(4));
// Check workflow ID
def wf = componentManager.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProject()).get("id").toString();
if (Double.parseDouble(wf) == 11600 ||
Double.parseDouble(wf) == 11601)
{
   
// Set Cost Unit
def CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
def CustomField parentcustomField = customFieldManager.getCustomFieldObjectByName("Cost Unit");
def Option parentnrd = (Option)parentIssue.getCustomFieldValue(parentcustomField);
def FormField cu = getFieldById(getFieldChanged());
cu.setFormValue(parentnrd.getOptionId());
    
}
}

 

Best regards,

 

Jo

 

Tatiana Jancusova January 24, 2018

Hello CGM,

I was trying to use your script, but I get the 'unable to get the class FormField' message

@ line 08, 22 column 15

I am using:

JIRA server v7.2.7

Adaptavist ScriptRunner for JIRA v4.3.19

 

Can you help me?

Best regards,
Tatiana

CGM January 24, 2018

Hello Tatiana,

we changed the script because it stopped working after upgrading to 7.x.

The new version is working but you have to change the workflow ID to your ID and you need to have the JIRA Software version and not JIRA Core.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.opensymphony.workflow.loader.StepDescriptor
import com.onresolve.jira.groovy.user.FormField
 
if (getFieldById("parentIssueId") != null)
{
 def issueid = getFieldById("parentIssueId").value;
 if (issueid != null)
 {
  def Long issueIdLong = java.lang.Long.parseLong(issueid.toString());
  def Issue parentIssue = ComponentAccessor.getIssueManager().getIssueObject(issueIdLong);
  if(parentIssue != null)
  {
   // Check Workflow ID
   if (Double.parseDouble(ComponentAccessor.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProjectObject()).get("id").toString()) == 12402 ||
    Double.parseDouble(ComponentAccessor.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProjectObject()).get("id").toString()) == 12401)
   {
// Set FixVersion
def fixVersions = parentIssue.getFixVersions();
def FormField fv = getFieldById("fixVersions");
fv.setFormValue(fixVersions*.id);
//if (fixVersions != null) fillenrd.setHelpText(fixVersions.iterator().next().getName());
    
// Set Components
def components = parentIssue.getComponents();
def FormField c = getFieldById("components");
c.setFormValue(components*.id);
    
//fillenrd.setHelpText(componentManager.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProject()).get("id").toString());
}
}
}
}

Best regards,

Joachim

Tatiana Jancusova February 13, 2018

Hello Joachim,

Thank you very much for your update. Do you have also the script for the epic?

Regards,
Tatiana

CGM February 13, 2018

Hello Tatiana,

sorry my fault. Here is the script for the Epic:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FormField

def FormField field = getFieldByName("Epic Link");
if (field != null)
{
 def Issue parentIssue = ComponentAccessor.getIssueManager().getIssueObject(field.getValue().toString().substring(4));
 if (parentIssue != null)
    {
  // Check Workflow ID
  def wf = ComponentAccessor.getWorkflowSchemeManager().getWorkflowScheme(parentIssue.getProjectObject()).get("id").toString();
  if (Double.parseDouble(wf) == 12401 || Double.parseDouble(wf) == 12402)
  {
      // Set Cost Unit only when creating issues
   if (getActionName().equals("Create"))
            {
    def CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
    def CustomField parentcustomField = customFieldManager.getCustomFieldObjectByName("Cost Unit");
    def Option parentnrd = (Option)parentIssue.getCustomFieldValue(parentcustomField);
    def FormField cu = getFieldById(getFieldChanged());
    cu.setFormValue(parentnrd.getOptionId());
   }
  }
 }
}
1 vote
andreas
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 21, 2016

There's now a very easy to configure way to copy field values from epics to stories using Automation for JIRA!

You can easily modify related issues (such as parents, stories, sub-tasks, epics, linked issues and even JQL) using our related issues action and condition:

(this example transitions the stories in the epic, but you could just as easily edit the stories in the epic and set values based on the fields in the epic)

For more details see https://blog.codebarrel.io/synchronize-parent-and-sub-task-issues-with-automation-for-jira-bdcca6c9d453

1 vote
Robert
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 23, 2015

Jo,

Epic link and Epic name are fields in story. So you can use searchService to find issues using JQL query.

using parentIssue or issue.getParentObject() will now work in this case. It only works for Issues and subtasks. Here you can find some code you use:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.search.SearchResults

User user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser();
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);

String jqlSearch="here put your jql";
Long counter=null;
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);
if (parseResult.isValid()) {
    SearchResults results=searchService.search(user, parseResult.getQuery(),PagerFilter.getUnlimitedFilter())
    counter = results.getTotal();
    log.error("number of results:"+counter+" for query: "+jqlSearch);
} 
else {
    log.error("Incorrect JQL query: " + jqlSearch);


}
//process results
0 votes
CGM July 27, 2015

Thank you both for your fast answer. We will test Roberts script and have a look at the IssueLinkManager in the next days.

When creating a Story from Epic, I can see the name and ID of the epic in the create dialog so the information must be there:

image2015-7-28 8:46:23.png

We only have to find it smile

Best regards,

Jo

JamieA
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 28, 2015

I can't see it working with Robert's script. You're right, the page knows about it, but it's not being passed to your behaviours script.

0 votes
JamieA
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 27, 2015

Hrm... in your example you get the parentIssueId from the form, which is correct for getting the parent. But there is nothing available for getting the issue that the new issue will be linked to.

I don't think Robert's example will work because the epic link is not created until the issue is submitted. Even so, I'd probably use IssueLinkManager rather than jql.

You should be able to get the epic from the "epic link" field, but it doesn't seem to be present, perhaps because it's disabled.

I created https://jamieechlin.atlassian.net/browse/GRV-765 - please vote for it if you want to see it fixed.

 

 

Suggest an answer

Log in or Sign up to answer