Inward & Outward links in ScriptRunner script field?

Connor Kelly October 29, 2019

Hello Atlassian community! I'm trying to create a Scripted Field that will sum up the value of a number field on all linked issues, however so far I can only get it to work in one direction or the other. I've found some threads about including both Inward and Outward links in other contexts, but I can't get them to work for a Scripted Field.

Has anyone set something like this up before, or have any insight? Here's the two versions of the scripts that work, one for each direction:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def linkedIssues = ComponentAccessor.issueLinkManager.getInwardLinks(issue.id)
if (!linkedIssues) {
return null
}

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().sourceObject).findByName('Number Custom Field')
if (!customField) {
log.debug "Custom field is not configured for that context"
return null
}

linkedIssues*.sourceObject.sum { Issue it -> it.getCustomFieldValue(customField) ?: 0 }
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id)
if (!linkedIssues) {
return null
}

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().destinationObject).findByName('Number Custom Field')
if (!customField) {
log.debug "Custom field is not configured for that context"
return null
}

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(customField) ?: 0 }

 

1 answer

1 accepted

1 vote
Answer accepted
Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 29, 2019

Hi,

Could you try the following?

 

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
def log = Logger.getLogger("com.nolddor") log.setLevel(Level.DEBUG) @PluginModule IssueLinkManager issueLinkManager
@PluginModule
CustomFieldManager customFieldManager //-------------------------------------------------------------- // You must change the following variables as your needs //-------------------------------------------------------------- def numericFieldName = "Number Custom Field" //--------------------------------------------------------------
//Retrieve customfields from the system
def numericField = customFieldManager.getCustomFieldObjectsByName(numericFieldName).find()

// Ensure source fields really exist
if(numericField)
{
// Look for all linked issues
def inwardLinkedIssues = issueLinkManager.getInwardLinks(issue.id)*.sourceObject
def outwardLinkedIssues = issueLinkManager.getOutwardLinks(issue.id)*.destinationObject

def linkedIssues = inwardLinkedIssues + outwardLinkedIssues

return linkedIssues.sum{ Issue linkedIssue -> linkedIssue.getCustomFieldValue(numericField) ?: 0 }

}

 

Regards

Connor Kelly October 30, 2019

Hi Jack!

I'm getting a "could not find matching method" for getCustomFieldValue on line 34. Which seems weird, since everything being imported suggests that should be recognized, and it's used in my current version of the script. Any idea why it wouldn't be recognized here?

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 30, 2019

It was my fault, I forgot to cast the closure item into an Issue class.

I've edited my previous message, could you try again please?

Thanks

Connor Kelly October 30, 2019

No worries, I missed that as well! Thank you very much, that worked :D

Pawel Ogrodnik July 21, 2021

@Jack Nolddor _Sweet Bananas_  This is exactly what I was looking for, it works great, I have an additional question, is it possible to add value from this task to the sum?

The same fields but from linked issues and this task?

 

Never mind, it was easy, just add ValA +

dD October 14, 2021

Hello this s eactly what i need but I receive following error message:

Cannot return value of type java.lang.Object on method returning type java.lang.Double

What does it mean and how can I solve this?

Suggest an answer

Log in or Sign up to answer