I'm new to using scriptrunner and scripting in general. I've been attempting to copy the value of an Epic's custom field to all of the Issues in Epic custom field of the same name. I've attempted this and am looking for guidance.
**Updated Code. No errors show, but linked issues custom fields are not being updated**
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def linkType = "Issue in Epic"
def linkMgr = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def event = event as IssueEvent
Issue issue = event.issue as Issue
def accountField = cfManager.getCustomFieldObjects(issue)?.find{it.name == "Account"}
def cfValue = issue.getCustomFieldValue(accountField)
def epicLink = cfManager.getCustomFieldObjectByName("Epic Link")
def epicName = cfManager.getCustomFieldObjectByName("Epic Name")
def change = event?.changeLog?.getRelated("ChildChangeItem")?.find{it.field == accountField}
if (!change){
return
}
def linkedIssue = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id).findAll{it.issueLinkType.name == linkType}
if (!linkedIssue){
return
}
if (change){
linkedIssue.each{
def linkedIssueObj = it.destinationObject
def oldValue = linkedIssueObj.getCustomFieldValue(accountField)
def newValue = issue.getCustomFieldValue(accountField)
accountField.updateValue(null, linkedIssueObj, new ModifiedValue(oldValue, newValue), new DefaultIssueChangeHolder())
}
}
You are missing a line to actually change the value on the issue.
linkedIssueObj.setCustomFieldValue(accountField, issue.getCustomFieldValue(accountField) )
But note that this will not cause your linkedIssue to show the history of that change. You will need to add that yourself if you care.
Alternatively, use issueManager.updateIssue() instead
Also, the linkedIssue will not be re-indexed so it will still come up when you search for the old value.
So your full script could look like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption
def linkType = "Issue in Epic"
def linkMgr = ComponentAccessor.issueLinkManager
def cfManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def event = event as IssueEvent
Issue issue = event.issue as Issue
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def accountField = cfManager.getCustomFieldObjects(issue)?.find{it.name == "Account"}
def cfValue = issue.getCustomFieldValue(accountField)
def epicLink = cfManager.getCustomFieldObjectByName("Epic Link")
def epicName = cfManager.getCustomFieldObjectByName("Epic Name")
def change = event?.changeLog?.getRelated("ChildChangeItem")?.find{it.field == accountField}
if (!change){
return
}
def linkedIssue = linkMgr.getOutwardLinks(issue.id).findAll{it.issueLinkType.name == linkType}
if (!linkedIssue){
return
}
if (change){
linkedIssue.each{
def linkedIssueObj = it.destinationObject
linkedIssueObj.setCustomFieldValue(accountField, cfValue)
issueManager.updateIssue(currentUser, linkedIssueObj , EventDispatchOption.DO_NOT_DISPATCH, false)
def wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(linkedIssueObj)
ImportUtils.setIndexIssues(wasIndexing)
}
}
Thank you for your reply!
I've placed this code in the Listener and it seems the
issueManager.updateIssue(currentUser, linkedIssueObj , EventDispatchOption.DO_NOT_DISPATCH, false)
code is throwing an error "Cannot find matching method". When I do the autofill "updateIssue" is an option.
This is the only error being returned. Do you have any idea why?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Static type checking errors don't usually mean that the code is bad.
If you want to remove those, you have to define every object with the appropriate type.
I'm not sure why updateIssue is not visible in the autocomplete, but it's definitely a valid method: https://docs.atlassian.com/software/jira/docs/api/8.0.1/com/atlassian/jira/issue/IssueManager.html#updateIssue-com.atlassian.jira.user.ApplicationUser-com.atlassian.jira.issue.MutableIssue-com.atlassian.jira.event.type.EventDispatchOption-boolean-
Try to run the code see how it goes.
You can run the same code in the console by changing the following 3 lines:
def event = event as IssueEvent
Issue issue = event.issue as Issue
/*...*/
def change = event?.changeLog?.getRelated("ChildChangeItem")?.find{it.field == accountField}
to
Issue issue = issueManager.getIssueObject("EPIC-1") //put an epic you want to test
/*...*/
def change = ['dummy']
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've been trying to test and change a few things, but it seems like the Issues in epic aren't updating. I'll try another method.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've been silent because I received updated information from the requestor of this action. It seems the individual would like to only update the "Account" field on a newly added Issue to the epic.
I'll mark this answer as correct.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.