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 }
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No worries, I missed that as well! Thank you very much, that worked :D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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 +
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.