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

Inward & Outward links in ScriptRunner script field?

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

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

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?

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

971 views 10 25
Read article

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