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

Serj Shcherbakov May 16, 2017

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

3 votes
Answer accepted
Serj Shcherbakov May 25, 2017

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
0 votes
Serj Shcherbakov May 24, 2017

Adaptivist Experts, i need you!
Please help

0 votes
Serj Shcherbakov May 24, 2017

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.

Vineela Durbha April 4, 2019

Hi @Serj Shcherbakov 

I am trying to set a customfield value for a date field in the linked ticket as below

IssueLinkManager linkManager = ComponentAccessor.getIssueLinkManager();
List<IssueLink> issueLinks = linkManager.getOutwardLinks(issue.getId());

for(IssueLink link : issueLinks) {
log.debug("link name " +link.getDestinationObject().getStatus().getName())

if (link.getIssueLinkType().getName().equals("XXX)")){

Issue duplicateTicket = link.getDestinationObject();


duplicateTicket.setCustomFieldValue(plannedregDate,issue.getCustomFieldValue(plannedrcdate))

 

Is there anything wrong I am doing. Can you please help

0 votes
Serj Shcherbakov May 23, 2017

Up topic)

0 votes
Serj Shcherbakov May 17, 2017

Guys, your assistance still required in this case.

0 votes
Serj Shcherbakov May 17, 2017

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,

steg17 September 20, 2017

YES

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events