Forums

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

Default epic to newly created tickets , but unable to assign different EPIC while creating new one

Lakshmi CH
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.
April 8, 2021

Hi Team,

I have a request to add every "Task" issue assign to one specific epic link. I have used below code and working excepted, but currently when creating a new task, even if we assigned a different epic to a task, at the time of creation it automatically changes it and assigns mentioned epic the script (EPIC-1). we only want EPIC-1 assigned when the field it left blank during the ticket creation. Could you please suggest on this.

The original request of this requirement is : https://community.atlassian.com/t5/Jira-Software-questions/Default-epic-to-newly-created-tickets/qaq-p/1642030#M129927

 

Link : https://library.adaptavist.com/entity/automatically-add-epic-link-into-issue-of-certain-type

import com.atlassian.jira.component.ComponentAccessor

// When you create an issue of issue type Story that will be under an Epic
final issueTypeName = "Story"

// The issue key of the Epic
final epicIssueKey = "EPIC-1"

if (issue.issueType.name != issueTypeName) {
return
}

def epicLinkCustomField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName("Epic Link")
def newEpic = ComponentAccessor.issueManager.getIssueByCurrentKey(epicIssueKey)

issue.setCustomFieldValue(epicLinkCustomField, newEpic)

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 8, 2021

Hi @Lakshmi CH

You will need to modify the script slightly to suit your requirements.

In your code, I have observed, there is no condition to check if the Epic Link field is empty or not.

You should include something like:-

def epicLinkCustomField = customFieldManager.getCustomFieldObjects(issue).findByName("Epic Link")

if
(epicLinkCustomField.getValue(issue).toString() == "null") {
...
}

to ensure that the Epic Link field will be auto-filled only if it is empty.

Also, it would help if you used the Issue Manager to update the Issue as shown below:-

issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

to ensure that the change to the Epic Link field is updated.

Below is a fully working example script for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

final issueTypeName = "Task"
def epicIssueKey = "ET-1"

def epic = issueManager.getIssueByCurrentKey(epicIssueKey)
def epicLinkCustomField = customFieldManager.getCustomFieldObjects(issue).findByName("Epic Link")

if (issue.issueType.name != issueTypeName) {
return
}

if(epicLinkCustomField.getValue(issue).toString() == "null") {
issue.setCustomFieldValue(epicLinkCustomField, epic)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}

Please note this example code is not 100% exact to your environment. Hence, you will need to make the required modifications.

 

I hope this helps to solve your question :)

 

Thank you and Kind Regards,

Ram 

Suggest an answer

Log in or Sign up to answer