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

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

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 Community Leader Jan 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

@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

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())
    }
}
Elifcan Cakmak Community Leader Jan 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

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

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

@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 .

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.

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.

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

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 Community Leader Jan 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. 

@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 Community Leader Jan 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

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

@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

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
TAGS

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