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

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

This widget could not be displayed.

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> ?

This widget could not be displayed.

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

This widget could not be displayed.

Paulina

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

import com.atlassian.jira.component.ComponentAccessor;

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

log.debug("Issue ${issue}")
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink -&gt;
    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

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

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

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 = {-&gt; false}
log.debug("${customField.id}")
 
log.debug("Issue ${issue}")
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink -&gt;
 
//below I tried to exclude Story from TEST project

    if ((issueLink.issueLinkType.name == "Epic-Story Link" &amp;&amp; (!(issue.projectObject.key in ["TEST"] &amp;&amp; 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 

 

 

Hi to all,

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

Regards,
Mathieu 

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 -&gt;
    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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

45 views 2 2
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you