Forums

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

Adaptavist Scriptrunner: Update Custom Field Value of all Outward Links, Listener

Judah
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.
February 8, 2019

 

I need to update a custom field's value on all the child issues (feature issues) that are connected to a project, so that they match their parent whenever it (the project issue) is updated. 

I have been able to access all of the child issues of my given project.  My second 'log.debug' below returns a List (array?) of the children issues linked to the project 'DEVP'...

I need automatically update a custom field on ALL of these children issues when that same field is updated on the parent...

if (issue.projectObject.key == "DEVP"){

log.debug (issue)
log.debug (issueLinkManager.getOutwardLinks(issue.id))

//field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), field), changeHolder)

}

Can I implement updateValue() multiple issues at once? Do I need a loop?

Entire script: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.bc.issue.*
import static com.atlassian.jira.workflow.TransitionOptions.Builder;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLink
import org.apache.log4j.Logger
import org.apache.log4j.Level


MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("DEVP-384");
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

def changeHolder = new DefaultIssueChangeHolder()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager();

def field = customFieldManager.getCustomFieldObjects(event.issue).find { it.name == "Issue Category" }

def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issueCatValue = issue.getCustomFieldValue(field)

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

if (issue.projectObject.key == "DEVP"){

log.debug (issue)
log.debug (issueLinkManager.getOutwardLinks(issue.id))

//field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), field), changeHolder)

}

 I can access my Custom Field, the Project, and all of it's children...but I am having trouble putting these parts together. Thank you.

1 answer

1 accepted

0 votes
Answer accepted
Tuncay Senturk _Snapbytes_
Community Champion
February 8, 2019

As far as I know, you need a loop.

At least updateValue method expects single Issue as a parameter.

Judah
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.
February 21, 2019

Yes you are correct. A loop was necessary, as I needed to access each linked issue singularly and then update the field. 

Katrina Thacker March 17, 2020

@Judah would you mind sharing your final code? I need to do something similar.  Thanks so much!

Judah
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.
March 26, 2020

@Katrina Thacker For some reason this script is no longer in production and I cannot locate it. But this script is similar in that it defines some custom fields and sends changes down the chain when the parent field is updated

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.search.DocumentWithId
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.BusLine")
log.setLevel(Level.DEBUG)

Issue issue = event.issue

def key = issue.getKey()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def BLfield = customFieldManager.getCustomFieldObjects(event.issue).find { it.name == "Business Line" }
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def parentLink = customFieldManager.getCustomFieldObjectsByName('Parent Link')[0]
def customFieldValue = issue.getCustomFieldValue(parentLink)
def parentKey = (String) customFieldValue
def parentIssue = issueManager.getIssueObject(parentKey)
def parentValue = issue.getCustomFieldValue(BLfield)

// YOU CAN PUT WHATEVER IN THE BELOW LOG METHOD TO SEE WHAT THE VALUE OF YOUR FIELDS ARE AT RUNTIME

log.debug("ParentLink Value = " + customFieldValue)

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "issuetype in (Feature,FTS) and issuekey in childIssuesOf(" + key + ")"
def query = jqlQueryParser.parseQuery(jqlSearch)
def results = searchService.search(user,query, PagerFilter.getUnlimitedFilter())

// LOOP THROUGH THE RESULTS
results.getResults().each {documentIssue ->;

def childIssue = issueManager.getIssueObject(documentIssue.id)
def changeHolder = new DefaultIssueChangeHolder()
def childValue = childIssue.getCustomFieldValue(BLfield)
def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)

//UPDATE THE FIELD'S VALUE
BLfield.updateValue(null, documentIssue, new ModifiedValue(childValue, parentValue), changeHolder)

issueIndexManager.reIndex(documentIssue)
}

Suggest an answer

Log in or Sign up to answer