Forums

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

Scriptrunner - update all linked issues when parent is updated and vice versa

NBE September 27, 2018

Hello,

I need to implement two separate behaviors as script listeners or post functions:

  1. When the custom field "Scheduled Release" is updated on the story, copy it to all the story's linked issues. Our stories will usually have bugs and tasks linked to them using different relation types ("blocked by", "relates to" etc).
  2. When a new bug or task is created, and is linked to a story, fetch the parent issue's (story) "Scheduled Release" value, and copy it to the bug/task which were just linked. When an existing bug / task are linked to a story, do the same. The difference here is that the copying of "Scheduled Release" should be done when the bug / task was first linked to its parent. This event will not necessarily occur upon the creation of the bug / task. 

I am new to scriptrunner and groovy, and have been looking for examples on how to implement this. Unfortunately I couldn't get anything working so far.

Any help from the community will be much appreciated. 

Thank you! 

1 answer

0 votes
Gökhan Çöplüoğlu September 27, 2018

Hi,

Maybe this helps you:

 

/**
* Created by f542 on 9/18/2018.
*/
//import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
//import com.atlassian.jira.bc.issue.search.SearchService
//import com.atlassian.jira.component.ComponentAccessor
//import com.atlassian.jira.issue.ModifiedValue
//import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
//import com.atlassian.jira.issue.CustomFieldManager
//import com.atlassian.jira.issue.Issue
//import com.atlassian.jira.issue.IssueManager
//import com.atlassian.jira.issue.fields.CustomField
//import com.atlassian.jira.issue.search.SearchResults
//import com.atlassian.jira.issue.watchers.WatcherManager
//import com.atlassian.jira.user.ApplicationUser
//import com.atlassian.jira.user.util.UserManager
//import com.atlassian.jira.issue.MutableIssue
//import com.atlassian.jira.event.type.EventDispatchOption
//import com.atlassian.jira.web.bean.PagerFilter
//import com.atlassian.query.Query

ApplicationUser adminUser = ComponentAccessor.getUserManager().getUserByName("admin")
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class)
IssueManager issueManager = ComponentAccessor.getIssueManager()
UserManager userManager = ComponentAccessor.getUserManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
ApplicationUser userApp
def i=0
try {
CustomField developerUserCF = customFieldManager.getCustomFieldObject(24196L)

MutableIssue missue = ComponentAccessor.getIssueManager().getIssueByKeyIgnoreCase("XYZ-1234")
//Issue missue = ComponentAccessor.getIssueManager().getIssueByKeyIgnoreCase("XYZ-1234")

def developerUserName = missue.getCustomFieldValue(developerUserCF)
if(developerUserName == null || developerUserName==""){

issueLinkManager.getOutwardLinks(issue.id).each{ issueLink ->
Issue linkedIssue = issueLink.getDestinationObject()

String devUser = (String)linkedIssue.getCustomFieldValue(developerUserCF)//LINKED ISSUE DEVELOPER USER

if(devUser!=null){
if(issueLink.getIssueLinkType().getId() == 10130){

log.error(issue.getKey()+" - "+linkedIssue+" - "+devUser)
log.error(issueLink.getIssueLinkType().getName())
userApp = userManager.getUserByName(devUser)

FieldLayoutItem fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(missue).getFieldLayoutItem(developerUserCF)
Object parentValue = userApp
Object currentValue = issue.getCustomFieldValue(developerUserCF)
ModifiedValue modifiedValue = new ModifiedValue( currentValue, parentValue )
developerUserCF.updateValue( fieldLayoutItem, missue, modifiedValue, new DefaultIssueChangeHolder() )
missue.store()

}
}
}
}



} catch (Exception excp) {
log.error(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(excp))
}

This script update linked issue's user field by issueTypeId 

Suggest an answer

Log in or Sign up to answer