Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Sum of Story Points in Epic - scripted field

Maya_Chase October 22, 2018

I'm trying to implement the scripted field per the discussion here: https://community.atlassian.com/t5/Jira-questions/Scripted-field-sum-of-story-points-of-linked-issues-only-with/qaq-p/33025

And the script I'm using looks like:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
def componentManager = ComponentManager.getInstance()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
double totalSP = 0
customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11662");

if (issue.getIssueTypeId() != 6) {
return null
}
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->;
if (issueLink.issueLinkType.name == "Epic-Story Link" ) {
double SP = (double)(issueLink.destinationObject.getCustomFieldValue(customField) ?: 0)
totalSP = SP + totalSP;
}}
return totalSP

-----------------

The Story Points field ID is 11662, and the issue type ID for Epic is 6. 

The script compiles and runs, but always returns null. For some reason, logging does not work for this script, so I'm kind of flying blind. Anyone see anything obvious?

Thanks!

3 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Pete Singleton
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.
November 2, 2019

I managed to get this working (using JMCF add-on) as follows:

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

double totalSP = 0
customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points");
enableCache = {-> false}

issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
if (issueLink.issueLinkType.name == "Epic-Story Link" ) {
int SP = (int)(issueLink.destinationObject.getCustomFieldValue(customField) ?: 0)
log.debug("Story Points : "+ SP);
log.debug("Issue :" + issueLink.getDestinationObject().getKey() );
totalSP = SP + totalSP;
}}

return totalSP
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2019

Note that this is with JMCF 1.7.  In JMCF 2, it would be one line of code. 

Sara Shoemaker December 26, 2019

Could you tell me what that line of code would be?

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 27, 2019

Sure:

issue.stories?.sum { it.get("Story Points", 0) }

Be aware, though, that the "Story Points" field might be called differently on your instance (such as "Story points" - lowercase "p"). The safest is to use the custom field ID instead of name, which is accessible from the "Issue Fileds" help tab of the formula editor.

Chris Melville December 10, 2021

What would the equivalent JMCF code be for summing up the story points in an Initiative that contains Epics with story points?

Ideally, I would have one formula that works for either Initiatives or Epics, so I don't have to create a new context for the two issue types.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 13, 2021
issue.portfolioChildIssues?.sum { it.get("Story Points", 0) }

This should actually work for both.

David

Like Chris Melville likes this
Chris Melville December 14, 2021

Thank you for the response.  It does work for Initiatives and other Portfolio hierarchy issue types, but does not work for Epics, even though they are defined in the hierarchy.  I can solve for both by simply combining the two.

0 votes
Danil Chernyshev November 1, 2019
Not sure, but maybe something wrong with typeID or feldID. Could you try this:




//Scripted Fields
//Field name : StoryPointsSum
//Field Description : Sum of Story Points in Epic
//Default Configuration Scheme for Count of Open Stories
//Template : Number Field

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
def componentManager = ComponentManager.getInstance()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
double totalSP = 0




customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points");

if (issue.getIssueType().getName() != "Epic") {
return null
}

issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->


if (issueLink.issueLinkType.name == "Epic-Story Link" ) {
totalSP += (double)(issueLink.destinationObject.getCustomFieldValue(customField) ?: 0)
}
}

return totalSP 
0 votes
Pete Singleton
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.
July 17, 2019

Hi, I'm also trying to sum the story points and show the total on Epics via scripted fields.  Did you get this working??

TAGS
AUG Leaders

Atlassian Community Events