Update Fix Version/s of the Linked Issue using Script Listner

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 25, 2021

Hi Guys,

 

Need one help in Updating Fix Version of the Linked Issue. Issue Link type = "Covers". 
I have tried to write a code, but it 's not working throwing an error. Please help me getting this fixed. ErrorListenr.PNGListner Error.PNG

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

final customFieldName = 'Fix Version/s'

Issue fix_Issue = event.getIssue()
if (fix_Issue.issueTypeObject.name == "Story" )
{
List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} )
{
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()

}

def linkedIssues = ComponentAccessor.issueLinkManager
.getOutwardLinks(issue.id)
.findAll { it.issueLinkType.name in ['Covers'] && it.destinationObject.issueType.name == "Story" }


if (!linkedIssues) {
return
}

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.find {
it.name == customFieldName
}

linkedIssues.each {
def linkedIssue = it.destinationObject
def newValue = fixVersions
customField.updateValue(null, linkedIssue, new ModifiedValue(newValue), new DefaultIssueChangeHolder())
}

2 answers

2 accepted

1 vote
Answer accepted
Elifcan Cakmak
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.
January 26, 2021

Hello,

You didn't close the curly braces of the if you started, that's why it throws exception:

if (fix_Issue.issueTypeObject.name == "Story" )
{

You need to add one closing } at the end.

Cheers.

Elifcan

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Elifcan Cakmak  I have tried to close the IF, but new error :( 

Please help me where am i doing wrong.  Can you please check the logic once.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

final customFieldName = 'Fix Version/s'

Issue fix_Issue = event.getIssue()

if (fix_Issue.issueTypeObject.name == "Story" ) {
List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} )
{
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()
}
}

def linkedIssues = ComponentAccessor.issueLinkManager
.getOutwardLinks(issue.id)
.findAll { it.issueLinkType.name in ['Covers'] && it.destinationObject.issueType.name == "Story" }

if (!linkedIssues) {
return
}

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.find {
it.name == customFieldName
}

linkedIssues.each {
def linkedIssue = it.destinationObject
def newValue = fixVersions
customField.updateValue(null, linkedIssue, new ModifiedValue(newValue), new DefaultIssueChangeHolder())
}

0 votes
Answer accepted
mogavenasan
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.
January 26, 2021

Hi @Vikrant Yadav,

Your script's indentation is really confusing, I hope I got the logic correct. The error is stating that it's expecting '}', but found nothing. Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

final customFieldName = 'Fix Version/s'


Issue fix_Issue = event.getIssue()


if (fix_Issue.issueTypeObject.name == "Story" ) {
    List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
    if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} ) {
        Collection<Version> fixVersions = new ArrayList<Version>();
        fixVersions = fix_Issue.getFixVersions()
    }

    def linkedIssues = ComponentAccessor.issueLinkManager
    .getOutwardLinks(issue.id)
    .findAll { it.issueLinkType.name in ['Covers'] && it.destinationObject.issueType.name == "Story" }

    if (!linkedIssues) {
        return
    }

    def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.find {
        it.name == customFieldName
    }

    linkedIssues.each {
        def linkedIssue = it.destinationObject
        def newValue = fixVersions
        customField.updateValue(null, linkedIssue, new ModifiedValue(newValue), new DefaultIssueChangeHolder())
    }
}
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Elifcan Cakmak @mogavenasan 

Now getting new error :(New er.PNGNew Error.PNG

Elifcan Cakmak
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.
January 26, 2021

You need to import issue and version 

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.project.version.Version

Like # people like this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Elifcan Cakmak  @mogavenasan  thanks for the suggestion. It's not working.

Fix Version of Linked Issue is not updating. :(Fix Version HBB.PNGVikrant fix.PNG

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

final customFieldName = 'Fix Version/s'

Issue fix_Issue = event.getIssue()

if (fix_Issue.issueTypeObject.name == "Story" ) {
List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} ) {
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()
}

def linkedIssues = ComponentAccessor.issueLinkManager
.getOutwardLinks(issue.id)
.findAll { it.issueLinkType.name in ['Covers'] && it.destinationObject.issueType.name == "Story" }

if (!linkedIssues) {
return
}

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.find {
it.name == customFieldName
}

linkedIssues.each {
def linkedIssue = it.destinationObject
def newValue = fixVersions
customField.updateValue(null, linkedIssue, new ModifiedValue(newValue), new DefaultIssueChangeHolder())
}
}

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Tom Lister @Nic Brough -Adaptavist- @Martin Bayer _MoroSystems_ s_r_o__  Please Suggest my script is not working.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

Do you really have a custom field called "Fix Version/s"?

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Nic Brough -Adaptavist-  No, i have Fix Version/s system field, i am trying to get Fix Version/s field value and paste it in linked issue Fix Version/s field .

mogavenasan
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.
January 26, 2021

Have you debug your script? For example, you are trying to get the Fix Version of the Jira issue, have you tried to log the value and verify if those variables are getting the correct values?

if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} ) {
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()
}

On the execution, you should log the fixVersions value and see if the script is getting the value.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

Ok, so there was a big clue in my question then.  Stop trying to fetch data from a field that is not a custom field using a function that can only fetch data from custom fields.

Elifcan Cakmak
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.
January 27, 2021

Hi @Vikrant Yadav Try it like this, I tried and it works:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.event.type.EventDispatchOption

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import org.apache.log4j.Logger
import org.apache.log4j.Level
def logger = Logger.getLogger("com.acme.CreateSubtask")
logger.setLevel(Level.DEBUG)
def issueManager = ComponentAccessor.getIssueManager()
def versionManager = ComponentAccessor.getVersionManager()
final customFieldName = 'Fix Version/s'
Issue fix_Issue = event.getIssue()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
Collection<Version> fixVersions
def user = authenticationContext.getLoggedInUser()

if (fix_Issue.issueTypeObject.name == "Story" ) {
List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} ) {
fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()
logger.debug fixVersions
}
//return fixVersions
def linkedIssues = ComponentAccessor.issueLinkManager
.getOutwardLinks(issue.id)
.findAll { it.issueLinkType.name in ['Covers'] && it.destinationObject.issueType.name == "Story" }

if (!linkedIssues) {
return
}

linkedIssues.each {
def linkedIssue = it.destinationObject
logger.debug linkedIssue
def newValue = fixVersions
versionManager.updateIssueFixVersions(linkedIssue, newValue)
issueManager.updateIssue(user, linkedIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

 

Like Vikrant Yadav likes this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2021

Hi @Elifcan Cakmak  Thanks a lot for your time and efforts. 

I have tested the script on my side. It's running, showing no failure but not updating the Fix Version/s field of the linked issue. Am i using Script Listener  in a wrong way? please suggest. Both source and destination projects are different. But issue type is same in both projects.Community Script.PNGDestination.PNGSource Project.PNG

I have attached screenshot of the source and destination project. 

Elifcan Cakmak
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.
January 27, 2021

You should look at the logs that I put in there, can the script logs the fix versions and the linked issues? Also check both your projects on the configuration screen. 

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2021

@Elifcan Cakmak  in Logs, listener is logging only Field value not the linked issue 

 

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2021-01-28 01:58:27,992 DEBUG [acme.CreateSubtask]: [HBB V2.15.2]

I have added both the projects in Project field in script listener.

Elifcan Cakmak
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.
January 28, 2021

Put a logger in the if !linkedissues part like logger.debug "no linked issues", let's see if it can't find the linked issues. I don't know what else to say without seeing and testing on your env, it worked for me with the same configuration. Try to print logs in between lines. That helps. 

Like # people like this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2021

Hi @Elifcan Cakmak  I have tried to add logger.debug , get below logs :-

Time (on server): Sun Jan 31 2021 20:26:40 GMT+0530 (India Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2021-01-31 08:56:40,999 DEBUG [acme.CreateSubtask]: [HBB V2.16.2]
2021-01-31 08:56:40,999 DEBUG [acme.CreateSubtask]: no issue
mogavenasan
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 1, 2021

@Vikrant Yadav,

Check what does the getOutwardLinks() returns before findAll{}

def linkedIssues = ComponentAccessor.issueLinkManager
.getOutwardLinks(issue.id)

logger.debug linkedIssues
Like Vikrant Yadav likes this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 1, 2021

Hi @mogavenasan @Elifcan Cakmak  This time it works for me great. Finally it get fixed.


Thanks a lot guys for your time and efforts :)

 

Time (on server): Mon Feb 01 2021 21:54:56 GMT+0530 (India Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2021-02-01 10:24:56,957 DEBUG [acme.CreateSubtask]: [HBB V2.16.2, HBB V2.17.0]
2021-02-01 10:24:56,957 DEBUG [acme.CreateSubtask]: []
2021-02-01 10:24:56,957 DEBUG [acme.CreateSubtask]: VY-5

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.event.type.EventDispatchOption

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import org.apache.log4j.Logger
import org.apache.log4j.Level
def logger = Logger.getLogger("com.acme.CreateSubtask")
logger.setLevel(Level.DEBUG)
def issueManager = ComponentAccessor.getIssueManager()
def versionManager = ComponentAccessor.getVersionManager()
final customFieldName = 'Fix Version/s'
Issue fix_Issue = event.getIssue()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
Collection<Version> fixVersions
def user = authenticationContext.getLoggedInUser()

if (fix_Issue.issueTypeObject.name == "Story" ) {
List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} ) {
fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()
logger.debug fixVersions
}
//return fixVersions
def linkedIssues = ComponentAccessor.issueLinkManager
.getOutwardLinks(issue.id)
logger.debug linkedIssues
.findAll { it.issueLinkType.name in ['Covers'] && it.destinationObject.issueType.name == "Story" }

log.info(linkedIssues)
if (!linkedIssues) {
logger.debug "no issues"
return
}

linkedIssues.each {
def linkedIssue = it.destinationObject
logger.debug linkedIssue
def newValue = fixVersions
versionManager.updateIssueFixVersions(linkedIssue, newValue)
issueManager.updateIssue(user, linkedIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

Like mogavenasan likes this

Suggest an answer

Log in or Sign up to answer