Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Create Listener that Links 2 Issues when Custom Field Changed

Our Current Scenario:

  1. Scripted Single Issue Picker Field: "Client Project Category Name"
    1. This field resides on all issues within a project and provides a search of all active Client Project Category type issues within that project. 
  2. We need to create a "child of" link between the current issue and what ever issue the user selected from the "Client Project Category Name" custom field. 

This field can be updated at any time and is not tied to a workflow as a result. 

We looked at behaviors, but it appears that listeners may be the best place to do this. I am new to groovy and unsure of what to place into our script to create a child link between 2 issues when that custom field is changed. 

Any help would be appreciated. 

1 answer

1 accepted

Hi @Stephen Higgins 

I think something like this (untested) should work:

Edit: I got creative and if you don't have much scripting experience it will be easier for you

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.Field
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.parameters.annotation.FieldPicker
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.onresolve.scriptrunner.parameters.annotation.UserPicker

@FieldPicker(label = "Issue Picker", description = "The Issue Picker field the user is selecting from")
Field issuePicker
@ShortTextInput(label = "LinkType Name", description = "The Link Type name you want to create between the two issues. Eg: Child Of")
String linkTypeName
@UserPicker(label = "Admin User", description = "User that have edit permission in all projects")
ApplicationUser admin

Issue issueFrom = event.getIssue()
CustomField cfIssuePicker = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(issuePicker.getId())

if (cfIssuePicker.getName() in getFieldsChanged()*.getField()) {
Issue issueTo = cfIssuePicker.getValue(issueFrom) as Issue
IssueLinkType issueLinkType = ComponentAccessor.getComponent(IssueLinkTypeManager).getIssueLinkTypesByName(linkTypeName)?.first()
if (issueLinkType)
ComponentAccessor.getIssueLinkManager().createIssueLink(issueFrom.getId(), issueTo.getId(), issueLinkType.getId(), 1L, admin)
}

List<ChangeHistoryItem> getFieldsChanged() {
return ComponentAccessor.getChangeHistoryManager().getAllChangeItems(event.getIssue())?.findAll {
it?.getChangeGroupId() == event.getChangeLog()?.id
}
}

 Regards

You should pick "Update Issue" event in the scriptrunner listener list. It may not work if the issue is edited in a transition screen.

@Alejandro Suárez _Tecnofor_ this has been working great! I wanted to see if you had any additional insight into what changes we could make to this script to also copy values from the outward linked issue into the issue that had a change on the field. 

Example:

  1. Source issue selects a new parent issue in "Client Project Category Name"
  2. Listener creates the link between the 2 issues (as currently working)
  3. Listener also copies the following custom fields from the parent issue into the child issue.
    1. Senior Director of Client Services
    2. Director of Client Services

@Stephen Higgins this is the code:
Just to clarify, the values of that fields always will be copied to the linked issue, that means that if the values in the original issue are null, then it will erase the values in the linked issue (if there is any).

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.Field
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.parameters.annotation.FieldPicker
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.onresolve.scriptrunner.parameters.annotation.UserPicker

@FieldPicker(label = "Senior Director of Client Services", description = "Senior Director of Client Services Field")
Field seniorDirectorField
@FieldPicker(label = "Director of Client Services", description = "Director of Client Services Field")
Field directorClientField
@FieldPicker(label = "Issue Picker", description = "The Issue Picker field the user is selecting from")
Field issuePicker
@ShortTextInput(label = "LinkType Name", description = "The Link Type name you want to create between the two issues. Eg: Child Of")
String linkTypeName
@UserPicker(label = "Admin User", description = "User that have edit permission in all projects")
ApplicationUser admin

Issue issueFrom = event.getIssue()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfIssuePicker = customFieldManager.getCustomFieldObject(issuePicker.getId())

if (cfIssuePicker.getName() in getFieldsChanged()*.getField()) {
MutableIssue issueTo = cfIssuePicker.getValue(issueFrom) as MutableIssue
IssueLinkType issueLinkType = ComponentAccessor.getComponent(IssueLinkTypeManager).getIssueLinkTypesByName(linkTypeName)?.first()
if (issueLinkType) {
ComponentAccessor.getIssueLinkManager().createIssueLink(issueFrom.getId(), issueTo.getId(), issueLinkType.getId(), 1L, admin)
CustomField cfSeniorDirector = customFieldManager.getCustomFieldObject(seniorDirectorField.getId())
CustomField cfDirectorClient = customFieldManager.getCustomFieldObject(directorClientField.getId())
issueTo.setCustomFieldValue(cfSeniorDirector, issueFrom.getCustomFieldValue(cfSeniorDirector))
issueTo.setCustomFieldValue(cfDirectorClient, issueFrom.getCustomFieldValue(cfDirectorClient))
ComponentAccessor.getIssueManager().updateIssue(admin, issueTo, EventDispatchOption.ISSUE_UPDATED, false)
}

}

List<ChangeHistoryItem> getFieldsChanged() {
return ComponentAccessor.getChangeHistoryManager().getAllChangeItems(event.getIssue())?.findAll {
it?.getChangeGroupId() == event.getChangeLog()?.id
}
}

Hello,
How can I edit this script for the linked issue in assigned users?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Opsgenie

Investigate deployment-related incidents with Bamboo and Opsgenie together

Back in April of last year one of the major product announcements from Opsgenie was the launch of the Incident investigation view which created a deep connection between Bitbucket and Opsgenie, empow...

93 views 1 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you