How to set an epic link in a script

Christoph Hilmes December 5, 2013

Hi,

i wonder how i can set the value of an epic link-field of an issue by script. I`m currently writing a script using it with script runner in a post function to set the epic link of a newly created issue to a certain value, according to the issue-type. This works when i set the value of a normal text-field. but i can t figure out what value to set for the epic-link field.

Can anybody help me out with this?

Thanks very much.

Christoph

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
TSO Logic Admin
Contributor
February 19, 2014

The "Epic Link" field is a Custom Field. So we have had some success by setting it just like any other custom field (from a Script Runner event listener in Groovy). Here are the key parts of code.

import com.atlassian.jira.component.ComponentAccessor
...

// Get Epic Link field - if none, we are done.
def issue = ...
def epicLinkField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find{it.name == 'Epic Link'}
if(!epicLinkField) {log.debug "No Epic Link field"; return}
log.debug "Existing Epic Link: ${epicLinkField.getValue(issue)}"

// Persist to DB desired value in Epic Link field.
// NOTE: issue.setCustomFieldValue(...) does NOT persist to DB.
String value = ...
if(!ListenerUtils.persistCustomFieldValue(ComponentAccessor.issueService, user, issue.id, epicLinkField, value, log)) {log.error "Failed to persist Epic Link"; return}
log.info "Issue $issue.key of issue-type '$issue.issueTypeObject.name' now has 'Epic Link' of '$value'"

import org.apache.log4j.Logger

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser

final class ListenerUtils {
...
public static boolean persistCustomFieldValue(IssueService issueService, ApplicationUser appUser, long issueId, CustomField customField, String value, Logger log) {
	assert issueService
	assert appUser
	assert issueId != null
	assert customField
	assert log
	
	def dirUser = appUser.directoryUser
	def uiip = addUpdateCustomFieldValue(createIssueInputParameters(issueService), customField, value)

	def updateValidationResult = issueService.validateUpdate(dirUser, issueId, uiip)
	if (!updateValidationResult.valid) {log.error "Update-validation result not valid: ${updateValidationResult.dump()}"; return false}

	def updateResult = issueService.update(dirUser, updateValidationResult)
	if (!updateResult.valid) {log.error "Update result not valid: ${updateResult.dump()}"; return false}

	return true
}

private static IssueInputParameters createIssueInputParameters(IssueService issueService) {
	assert issueService

	def iip = issueService.newIssueInputParameters()
	iip.skipScreenCheck = true
		iip.setRetainExistingValuesWhenParameterNotProvided true /* retainExistingValues */, true /* onlyValidatePresentFields */

	return iip
}

private static IssueInputParameters addUpdateCustomFieldValue(IssueInputParameters iip, CustomField customField, String value) {
	assert iip
	assert customField

	iip.addCustomFieldValue customField.id, value
	iip.providedFields = iip.providedFields ? iip.providedFields + customField.id : [customField.id]

	return iip
}

Hope that helps!

-Johnny

TAGS
AUG Leaders

Atlassian Community Events