Groovy Script Listener: field from inward linked issue(s) schould be updated. Edited

Hi, Everyone!

Guys, i try to solve my business case with groovy script listener but seems my attempts do not rich up the result.

What i have:

issue type named "SoW"
Field (Checkbox) named "Last Iteration" with value "Yes"
Link Name: "SoW iterations"; Inward Description: "is Next iteration of"; Outward Description "is Previous iteration of"

Case:

The field "Last Iteration" should be unchecked (cleared) in all (inward) linked issues when updating this field in the issue without outward links (is Previous iteration of)...

There is my script (A kind of similarity),

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

def issueLinkManager = ComponentAccessor.getComponent(IssueLinkManager)

if issue.issueTypeObject.name == 'SoW' && issueLinkManager.getOutwardLinks(issue.getId()).any {
it.issueLinkType.name == "SoW iterations"
}

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Last iteration'}
issue.setCustomFieldValue(cf, null)
}


Hope you help,



 

6 answers

1 accepted

Guys, now its work fine. Solved by myself, maybe useful to someone else:

//import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()

issueLinkManager.getOutwardLinks(issue.id).each { issueLink ->

if (issueLink.issueLinkType.name == "SoW iterations") {
System.err.println("links: " + issueLink.issueLinkType.name.toString())

def linkedIssue = issueLink.destinationObject
System.err.println("issue: " + issueLink.destinationObject.toString())

// Update "Last Iteration" field;

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cfLA = customFieldManager.getCustomFieldObjectByName("Last iteration")
def fieldConfig = cfLA.getRelevantConfig(linkedIssue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)

def cfLAvalue = linkedIssue.getCustomFieldValue(cfLA)

System.err.println("old value: " + linkedIssue + linkedIssue.getCustomFieldValue(cfLA))

if (cfLA) {
def changeHolder = new DefaultIssueChangeHolder()

cfLA.updateValue(null, linkedIssue, new ModifiedValue(cfLAvalue, [option]), changeHolder)

//inkedIssue.setCustomFieldValue(cfLA, option)

System.err.println("new value: " + linkedIssue + linkedIssue.getCustomFieldValue(cfLA))
}
}
}

true

Guys, I have modified my script, it helps solve errors from the log file, but seems still not working, here my new script:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.atlassian.jira.issue.MutableIssue
MutableIssue issue = issue

import com.atlassian.jira.issue.link.IssueLink
def componentManager = ComponentManager.getInstance()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()

issueLinkManager.getOutwardLinks(issue.id).findAll {
it.issueLinkType.name == "SoW iterations"
}.each {
// Update "Last Iteration" field;

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cfLA = customFieldManager.getCustomFieldObjectByName("Last iteration")
def fieldConfig = cfLA.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
issue.setCustomFieldValue(cfLA, [null])
}

 Hope you help,

Guys, your assistance still required in this case.

One more iteration of my script is present on a bottom. Now i try to solve behavior of my custom field: it is not updated in JIRA, but log says the value was updated. Maybe i miss something? 

 import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.link.IssueLink

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->

if (issueLink.issueLinkType.name == "SoW iterations") {
System.err.println("links: " + issueLink.issueLinkType.name.toString())

def linkedIssue = issueLink.destinationObject
System.err.println("issue: " + issueLink.destinationObject.toString())

// Update "Last Iteration" field;

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cfLA = customFieldManager.getCustomFieldObjectByName("Last iteration")
def fieldConfig = cfLA.getRelevantConfig(linkedIssue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)

def cfLAvalue = linkedIssue.getCustomFieldValue(cfLA)

System.err.println("old value: " + linkedIssue + linkedIssue.getCustomFieldValue(cfLA))

if (cfLA) {

linkedIssue.setCustomFieldValue(cfLA, "Yes")

System.err.println("new value: " + linkedIssue + linkedIssue.getCustomFieldValue(cfLA))
}
}
}

Log:

links: SoW iterations
issue: ZONTVCABO-119
old value: ZONTVCABO-119null
new value: ZONTVCABO-119Yes

Please help.

Adaptivist Experts, i need you!
Please help

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

344 views 0 1
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you