Forums

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

Scriptrunner Update Issues in Epic custom field

Raynard Rhodes
Contributor
June 19, 2019

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())

}
}


1 answer

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
June 21, 2019

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)
}
}
Raynard Rhodes
Contributor
June 21, 2019

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?

PD Sheehan
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.
June 21, 2019

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']
Raynard Rhodes
Contributor
June 28, 2019

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.

Raynard Rhodes
Contributor
July 8, 2019

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.

Suggest an answer

Log in or Sign up to answer