Scripted field - sum of story points of linked issues only with "Epic Link"

Paulina Wegrzyn December 11, 2015

Hi,

I found in other questions here, an answer which partially cover my question (https://answers.atlassian.com/questions/165598), but I want to have this to work only for linked issues with "Epic link", so I made some modification in code:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
 
def componentManager = ComponentManager.getInstance()
def issueLinkManager = componentManager.getIssueLinkManager()
def cfManager = ComponentManager.getInstance().getCustomFieldManager()
double totalSP = 0
 
customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points");
log.debug("${customField.id}")
 
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
    if (issueLink.issueLinkType.name == "Epic-Story Link") {
        def SP = issueLink.destinationObject.getCustomFieldValue(customField) ?: 0
        totalSP += SP
    }}
return totalSP

But it does not seem to work - when changing Story Points of some issues liked with epic link this scripted custom field displays random values (when refreshing page this value changes).

Do you have any clue where I made a mistake? (nothing found in logs)

(JIRA version 6.4.12, Service Runner - 3.1.4, scripted field use: Number Searcher and Number Field Template)

 

Thanks,

Paulina

 

3 answers

0 votes
Thanos Batagiannis _Adaptavist_
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.
December 11, 2015

Paulina

Can you try the code below and tell me if this works for you?

import com.atlassian.jira.component.ComponentAccessor;

enableCache = {-> false}
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def totalSP = 0.0
log.debug("${customField.id}")

log.debug("Issue ${issue}")
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
    if (issueLink.issueLinkType.name == "Epic-Story Link") {
        customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points")
        def SP = issueLink.destinationObject.getCustomFieldValue(customField) as Double ?: 0.0
        log.debug("SP value ${SP}")
        totalSP += SP
    }}

return totalSP as Double
Paulina Wegrzyn December 11, 2015

I checked this one and unfortunately have same situation. But what I observed - this scripted field is placed in view screen of Epic - as I wrote before when I am changing story points in stories (they are linked to this Epic with epic link), this situation with random values occurs, but then if I edit this Epic - correct value is displayed and when refreshing no random values appear - it stays in proper value. Is it possible that some kind of _indexing_ is missing from Epic level (as Epic is not edited but stories linked to it)? if yes how should I include it in this scripted field? Sorry if my explanation is not clear:) Paulina

Thanos Batagiannis _Adaptavist_
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.
December 15, 2015

Hi Paulina, Sorry for the delay, could you please try to disable caching ? https://scriptrunner.adaptavist.com/4.2.0.2-SNAPSHOT/jira/scripted-fields.html#_more_advanced_notes_on_caching

Paulina Wegrzyn December 16, 2015

Hi @Thanos Batagiannis [Adaptavist], Thanks very much for your support! It started to work after applying "enableCache = {-> false}" in my code . Do you know if this cache disabling have any influence on jira performance (we have very big production instance)? Also there is written in page which you sent to me that it should be fixed in 2.1.4 version (we are using ScriptRunner v 3.1.4). Br, Paulina

Paulina Wegrzyn February 5, 2016

Hi @Thanos Batagiannis [Adaptavist],

First, I want to thank you once again for your support and if you convert your comment to answer I will accept it.

However I have one question - after implementation of this field I need to limit it for Story issue type for one project (users of this project are using SP in different way as rest of users and they do not want to be counted in my field).

What I did? - I added condition to if loop as here:

import com.atlassian.jira.component.ComponentAccessor;
 
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def totalSP = 0.0
enableCache = {-> false}
log.debug("${customField.id}")
 
log.debug("Issue ${issue}")
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
 
//below I tried to exclude Story from TEST project

    if ((issueLink.issueLinkType.name == "Epic-Story Link" && (!(issue.projectObject.key in ["TEST"] && issue.issueTypeObject.name == "Story")))) {  
        customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Story Points")
        def SP = issueLink.destinationObject.getCustomFieldValue(customField) as Double ?: 0.0
        log.debug("SP value ${SP}")
        totalSP += SP
    }}
 
return totalSP as Double

 But it seems not to work properly - Story Points of Stories from TEST project are still counted in my field.

My scripted field "Sum of SP" is visible in Epic issue type and should display all Story Points of linked with "Epic link" issues except of Stories from TEST project.

What do you think, is it possible? if yes, where I made a mistake in my code?

 

Thanks in advance for your support!

We recently updated our Script Runner to 4.1.3.7 and must say that those additional functions are great!

 

Best regards,

Paulina 

 

 

Mathieu de Lafond August 4, 2016

Hi to all,

I have some trouble implementing this scripted field in JIRA 7.0 ... has anyone ever done it?

Regards,
Mathieu 

Mathieu de Lafond August 4, 2016

It's okay, I managed it after some research.

Just in case anyone needs it (adapted in case some user stories have no value at all) :

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_10259");

if (issue.getIssueTypeId() != "10 100") {
    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

Also the log. doesn't seem to work at all.

Maya_Chase October 19, 2018

I tried the above, and it compiles and executes with no error, but always returns null. I'm not really sure what the "10 100" is for, but I substituted 6 (no quotes), which is the ID for Epic in our system. I'd really like to get this working. Thanks!

Andy Ukasick July 8, 2021

I know this reply is about 3 yrs too late, but I needed to create a field like this and stumbled across this posting.  I needed to make some slight edits in the code above to get it working.  Here's what is working flawlessly for me now.  Be sure to put in the correct custom field id for your Story Point field.  Also, the Epic issue type normally has an id of 10000, but if yours is different, change that too.

Hopefully this will help someone.

 

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().getCustomFieldObject("customfield_10106");

if (issue.getIssueTypeId() != "10000") {
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

 

0 votes
Paulina Wegrzyn December 11, 2015

Hi @Thanos Batagiannis [Adaptavist], Thanks for quick reply - I changed it to 0.0 and it still it does not work... Yes, random values are displayed when viewing baseUrl/jira/browse/<issue> (only to this view screen my scripted field is added). By "random values" I mean - after change of story points of linked with epic link issue -> when refreshing issue page, once previous value is displayed, once correct one, after next change there are three values: 1st one, 2nd and correct one -and so on). Paulina

0 votes
Thanos Batagiannis _Adaptavist_
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.
December 11, 2015

Hi Paulina, Can you try to replace 0 with double values 0.0 (even though I am not sure that this is the reason)? Also in which screen/s you see random values, for example when you are viewing the baseUrl/jira/browse/<issue> ?

Suggest an answer

Log in or Sign up to answer