Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get custom field value from linked issues

Karol Urbaniak March 9, 2023

Hi, I need to obtain values of special custom-field from all linked issues (one custom-field = one linked issue) to the parent issue. Unfortunatetly when I run it my jira is looping and I'm forced to restart it. 

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

log.warn("PARENT JIRA ISSUE: " + issue)

def currentUser2 = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def linkedIssues = ComponentAccessor.issueLinkManager.getLinkCollection(issue, currentUser2).allIssues

def cField = ComponentAccessor.customFieldManager.getCustomFieldObject('customfield_24201')
def cFieldValue = issue.getCustomFieldValue(cField);
log.warn("PARENT ISSUE VALUE: " + cFieldValue)

for (value2 in linkedIssues){

    log.warn("LINKED JIRA ISSUE: " + value2)
    def issueLinked = ComponentAccessor.issueManager.getIssueByCurrentKey(value2.toString())
    log.warn("LINKED JIRA ISSUE PROJECT: " + issueLinked.projectId)
    if (issueLinked.projectId == 34343) {
        def cField2 = ComponentAccessor.customFieldManager.getCustomFieldObject('customfield_24201')
        def cField2Value = issueLinked.getCustomFieldValue(cField2);
        log.warn("LINKED JIRA ISSUE FIELD VALUE: " + cField2)
        //Here I would like to show value of the customfield of linked issue
        log.warn("Value2: " + cField2Value)
        cFieldValue += cField2Value
    }
}

//log.warn(linkedIssues)
if (cFieldValue == 0) {
    return null
}
else {
    return cFieldValue
}

2 answers

1 accepted

1 vote
Answer accepted
Florian Bonniec
Community Champion
March 9, 2023

Hi @Karol Urbaniak 

 

What are you trying to do ? What does "(one custom-field = one linked issue) to the parent issue" means ?

 

Regards

Karol Urbaniak March 9, 2023

Hi @Florian Bonniec 

it means that each linked issue have a single value of custom field 'customfield_24201'.

Regards

Florian Bonniec
Community Champion
March 9, 2023

What type of field is it and what do you want to do with the values from the sub elements ?

Karol Urbaniak March 9, 2023

Well, field is float and main goal is to sum values of all 'customfield_24201' in linked issues.

Unfortunately maybe code is not right to sum all fields but I was stopped in step to get properly the values so I haven't think about the next steps (to summarize them).

Florian Bonniec
Community Champion
March 10, 2023

This should work

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption

log.warn("PARENT JIRA ISSUE: " + issue)

def currentUser2 = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def linkedIssues = ComponentAccessor.issueLinkManager.getLinkCollection(issue, currentUser2).getAllIssues()

def cField = ComponentAccessor.customFieldManager.getCustomFieldObject('customfield_24201')
def issueManager = ComponentAccessor.getIssueManager()
double cFieldValue = 0.0
log.warn("PARENT ISSUE VALUE: " + cFieldValue)
MutableIssue issueUpdated = issue as MutableIssue
linkedIssues.each{ issueLinked ->
    log.warn("LINKED JIRA ISSUE: " + issueLinked.key)
    log.warn("LINKED JIRA ISSUE PROJECT: " + issueLinked.projectId)
    if (issueLinked.projectId == 34343) {
        cFieldValue += issueLinked.getCustomFieldValue(cField) ? issueLinked.getCustomFieldValue(cField) as double : 0.0
    }
}
issueUpdated.setCustomFieldValue(cField, cFieldValue)
issueManager.updateIssue(currentUser2, issueUpdated, EventDispatchOption.ISSUE_UPDATED, false)


//log.warn(linkedIssues)
if (cFieldValue == 0) {
    return null
}
else {
    return cFieldValue
}
Karol Urbaniak March 14, 2023

@Florian Bonniec  may I asked you for one thing. Code is working fine when it is being triggered during view pf an issue in browser. However if I list all issues in a project in Issue Navigator and add column with value of that field I'm receiving errors and column is empty. Problem is with the loop which gather values of field from linked issues:
44.79,10.200.31.198 /rest/issueNav/1/issueTable [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: BIIC-893, field: Total time consumption
java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:878)
at org.codehaus.groovy.runtime.ProxyGeneratorAdapter$InnerLoader.defineClass(ProxyGeneratorAdapter.java:846)
at org.codehaus.groovy.runtime.ProxyGeneratorAdapter.<init>(ProxyGeneratorAdapter.java:194)
at groovy.util.ProxyGenerator.lambda$createAdapter$0(ProxyGenerator.java:234)
at org.apache.groovy.util.concurrent.concurrentlinkedhashmap.ConcurrentLinkedHashMap.lambda$compute$0(ConcurrentLinkedHashMap.java:788)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
at org.apache.groovy.util.concurrent.concurrentlinkedhashmap.ConcurrentLinkedHashMap.compute(ConcurrentLinkedHashMap.java:800)
at org.apache.groovy.util.concurrent.concurrentlinkedhashmap.ConcurrentLinkedHashMap.computeIfAbsent(ConcurrentLinkedHashMap.java:777)
at org.codehaus.groovy.runtime.memoize.LRUCache.getAndPut(LRUCache.java:63)
at groovy.util.ProxyGenerator.createAdapter(ProxyGenerator.java:230)
at groovy.util.ProxyGenerator.instantiateDelegateWithBaseClass(ProxyGenerator.java:205)
at groovy.util.ProxyGenerator.instantiateDelegateWithBaseClass(ProxyGenerator.java:189)
at groovy.util.ProxyGenerator.instantiateDelegate(ProxyGenerator.java:181)
at groovy.util.ProxyGenerator.instantiateDelegate(ProxyGenerator.java:177)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:17939)
at org.codehaus.groovy.runtime.dgm$56.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1268)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1035)
at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:1017)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:1008)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:180)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.asType(ScriptBytecodeAdapter.java:603)
at Script2.run(Script2.groovy:16)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
at javax.script.ScriptEngine$eval.call(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:185)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$2.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:304)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$1.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScript(AbstractScriptRunner.groovy:316)
at com.onresolve.scriptrunner.runner.ScriptRunner$runScript$10.call(Unknown Source)
at com.onresolve.scriptrunner.customfield.GroovyCustomField$_getValueFromIssue_closure4.doCall(GroovyCustomField.groovy:278)
at com.onresolve.scriptrunner.customfield.GroovyCustomField$_getValueFromIssue_closure4.doCall(GroovyCustomField.groovy)
at jdk.internal.reflect.GeneratedMethodAccessor1109.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:274)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1035)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:38)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
at com.onresolve.scriptrunner.runner.diag.DiagnosticsManagerImpl$DiagnosticsExecutionHandlerImpl$_execute_closure1.doCall(DiagnosticsManagerImpl.groovy:397)


capture_error_jira.JPG

Florian Bonniec
Community Champion
March 14, 2023

You have implemented it as a script field ?

If so I think you have to go in each issue so it will set the value.

I usually avoid this kind of field so I'm not 100% aware of how they work.

Regards 

1 vote
Reece Lander _ScriptRunner - The Adaptavist Group_
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.
March 10, 2023

The HAPI feature introduced in ScriptRunner 7.11.0 should make this straightforward

Here is some sample code to sum the value of a "Counter" custom field on all linked issues:

 

issue.inwardLinks*.sourceObject.collect { linkedIssue -> 
linkedIssue.getCustomFieldValue('Counter')
}.sum()

Screenshot 2023-03-10 at 13.39.34.png

 

 

Cheers :)

Roshan Fernando August 27, 2023

Hi @Reece Lander _ScriptRunner - The Adaptavist Group_ ,

 

I'm trying to use your script to retrieve value from a linked issue customer field. However, It's not returning any value. Please see the attached screenshot. 

 

Thanks

Roshan
Capture.PNG

Suggest an answer

Log in or Sign up to answer