Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I automatically populate the Epic based on the selection of custom field values ?

Aisha M
Contributor
June 12, 2018

I want to automate the population of Epic link from the value selected during the creation of the issue.

Can someone help with the concept of how to achieve this ? Thanks

1 answer

0 votes
Tarun Sapra
Community Champion
June 12, 2018

Since, Epic link denotes the epic, thus it seems you intend to link the story/task to an epic via the epic link field based on a certain custom field value during the post-function of the create transition. I hope I have understood you right. 

You can't do this out of the box, but if you have a plugin like Script runner, then it's easily doable. 

PLease see here how to set epic link

https://community.atlassian.com/t5/Marketplace-Apps-questions/How-to-set-epic-link-in-Jira-using-Groovy/qaq-p/673054

How to check custom field value using groovy

https://community.atlassian.com/t5/Jira-questions/Check-custom-field-value-in-a-groovy-script/qaq-p/191997

Aisha M
Contributor
June 12, 2018

@Tarun Sapra Hello. Yes you got that absolutely right, . Also, I do have Scriptrunner available. Any help on how this could be accomplished using Scriptrunner ? Thank you

Tarun Sapra
Community Champion
June 12, 2018

Hello @Aisha M

Put the post-function in the very end of the list of post-functions available and go through the links which I shared as they have all relevant code. 

Aisha M
Contributor
June 13, 2018

@Tarun Sapra Okay Tarun. Thank you so much. Will try this out and see how it goes:) 

Tarun Sapra
Community Champion
June 13, 2018

Sure @Aisha M

If it works for you then please accept/upvote the answer so that others are helped as well.

Thanks,

Aisha M
Contributor
June 20, 2018

@Tarun Sapra Hi Tarun, I have added the post function and understood that the links from the scripts could be used for my requirement. But, how do I relate the two scripts, that is, if custom field value is abc, set epic to ABC ? Can you please give in your thoughts. Thanks 

Tarun Sapra
Community Champion
June 20, 2018

Hello @Aisha M

You don't need two scripts, in a single script you can fetch the custom field value as described in the links which I have shared and then if the value meets your condition then you can set epic link.

 

Psuedocode

If(yourCustomField value == what you want)  {

then
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())

}
Aisha M
Contributor
June 20, 2018

@Tarun Sapra Thank you Tarun. Is the below script meaningful,

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("ATOTTS4-107") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10259");
def value = (String)issue.getCustomFieldValue(customField);

if(CustomFieldValue==initial) {
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(ATOTTS4-107), epicIssue),new DefaultIssueChangeHolder())
}

 

Sorry for the trouble , really appreciate your help :)

Tarun Sapra
Community Champion
June 20, 2018

Hello @Aisha M

Yes, this script is meaningful, quick query is your customfield "customfield_10259" of type String? i.e. is it a text field?

Aisha M
Contributor
June 20, 2018

@Tarun Sapra Yes, its a text field. While I added this script, I get mini pop ups with this error:

The variable 'initial' is undeclared

The value 'ATOTTS4-107' is undeclared

Actually, initial is one of the field value and ATOTTS4 is the epic key

Tarun Sapra
Community Champion
June 20, 2018

The if statement should be

if(value == "initial")

instead of if(CustomFieldValue==initial)

And 

instead of

epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(ATOTTS4-107), epicIssue),new DefaultIssueChangeHolder())

you should write

epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue("ATOTTS4-107"), epicIssue),new DefaultIssueChangeHolder())

 

Also add an import for Component Accessor at the beginning.

import com.atlassian.jira.component.ComponentAccessor
Aisha M
Contributor
June 20, 2018

@Tarun Sapra The first statement is good now :)

But, getting the below error popup for the second statement,

com.atlassian.jira.issue.MutableIssue#getCustomFieldValue

(java.lang.string). Please check if the declared type is right & if the method exists

Also, added the import for Component Accessor :)

Tarun Sapra
Community Champion
June 20, 2018

If the custom field is of type string then it should work fine as you are type casting it to string, thus can you try executing the script

Aisha M
Contributor
June 20, 2018

@Tarun Sapra Hi Tarun :) The field is a drop down. It did not work. Dunno why. 

Also, I added this as the last step of post function at the create issue transition. I hope that's correct. 

Tarun Sapra
Community Champion
June 20, 2018

Ok, Could you please paste the complete code again in code macro and mention the type of each custom field being used in the code

Aisha M
Contributor
June 21, 2018

@Tarun Sapra

Custom Field Type - Drop down field

Field Value - initial

Epic NameATOTTS4-107

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("ATOTTS4-107") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10259");
def value = (String)issue.getCustomFieldValue(customField);

if(value == "initial") {
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue("ATOTTS4-107"), epicIssue),new DefaultIssueChangeHolder())
}

 

 

 

Tarun Sapra
Community Champion
June 21, 2018

Hello @Aisha M

Please try this code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def epicIssue = issueManager.getIssueObject("ATOTTS4-107") // Epic issue key here
def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10259");
def value = (String)issue.getCustomFieldValue(customField);

if(value == "initial") {
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue), new DefaultIssueChangeHolder());
}
Aisha M
Contributor
June 21, 2018

@Tarun Sapra The script has zero errors :) Thank you. But, the story is not updating with the Epic link when I select the field value. Dunno whats the problem here.

Aisha M
Contributor
June 21, 2018

@Tarun Sapra Below is the log report for the script failing.

 

2018-06-21 06:48:22,687 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-06-21 06:48:22,687 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ATOTTS4-111, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.link.DefaultIssueLinkManager.getIssueLink(DefaultIssueLinkManager.java:381)
 at com.atlassian.jira.issue.link.DefaultIssueLinkManager.createIssueLink(DefaultIssueLinkManager.java:89)
 at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
 at com.sun.proxy.$Proxy427.createIssueLink(Unknown Source)
 at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136)
 at com.sun.proxy.$Proxy427.createIssueLink(Unknown Source)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.createLink(EpicLinkManagerImpl.java:322)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.updateLinksForIssues(EpicLinkManagerImpl.java:238)
 at com.atlassian.greenhopper.manager.issuelink.EpicLinkManagerImpl.associateIssuesWithEpic(EpicLinkManagerImpl.java:179)
 at com.atlassian.greenhopper.service.issuelink.EpicServiceImpl.addIssuesToEpic(EpicServiceImpl.java:103)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.associateIssueWithEpic(EpicLinkCFType.java:686)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:202)
 at com.atlassian.greenhopper.customfield.epiclink.EpicLinkCFType.createValue(EpicLinkCFType.java:44)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script89.run(Script89.groovy:14)

Issue: ATOTTS4-111

{
    "full.module.key": "com.onresolve.jira.groovy.groovyrunnerrungroovy-function (java.lang.String)",
    "canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction (java.lang.String)",
    "class.name": "com.onresolve.jira.groovy.GroovyFunctionPlugin (java.lang.String)",
    "issue": "ATOTTS4-111 (com.atlassian.jira.issue.IssueImpl)",
    "passesCondition": "true (java.lang.Boolean)",
    "transientVars": {
        "entry": "com.opensymphony.workflow.spi.SimpleWorkflowEntry@18d1ff0a",
        "issue": "ATOTTS4-111 (com.atlassian.jira.issue.IssueImpl)",
        "configuration": "com.opensymphony.workflow.config.DefaultConfiguration@366e2859",
        "context": "com.opensymphony.workflow.basic.BasicWorkflowContext@527acb27",
        "createdStep": "SimpleStep@2[owner=, actionId=0, status=open] (com.opensymphony.workflow.spi.SimpleStep)",
        "originalissueobject": "null (org.codehaus.groovy.runtime.NullObject)",
        "actionId": "1 (java.lang.Integer)",
        "issueProperties": "{} (com.google.common.collect.EmptyImmutableBiMap)",
        "currentSteps": "[SimpleStep@2[owner=, actionId=0, status=open]] (java.util.ArrayList)",
        "pkey": "ATOTTS4 (java.lang.String)",
        "store": "com.opensymphony.workflow.spi.ofbiz.OfbizWorkflowStore@57d2f926",
        "descriptor": "com.atlassian.jira.workflow.ImmutableWorkflowDescriptor@1331c049"
    },
    "log": "org.apache.log4j.Logger@bd5b3c"
}
Tarun Sapra
Contributor
June 21, 2018

instead of last function of create transition can you add this post-function in the end at some other transition i.e. may be start progress or something but not create transition and share the result.

Aisha M
Contributor
June 25, 2018

@Tarun Sapra I don't have any other transition that would make sense. Its more like create - lots of status - Ready to accept - Done

Don't know where else to add this, anyway will give it a shot :)  

Aisha M
Contributor
July 17, 2018

@Tarun Sapra It did not work Tarun. Any other possibility of achieving this instead of using workflow post functions. Thank you :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events