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)
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.