Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,166
Community Members
 
Community Events
168
Community Groups

How to sum custom field values

Hello!

I have custom field 10508 and 10509. Each of them could be the numbers from 1 to 10.

The idea is to show calculated sum in the another custom field. As far as I know, I can reach this by adding a script in post function, so the sum will be calculated only after transaction of the issue, is it right? How this field can be updated on fly?

 

I tried this script, but it doesn't work:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

Issue issueKey = issue
def id=issueKey.getId()

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObject(10508)
def cff = customFieldManager.getCustomFieldObject(10509)

def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))

total = (sum = (n ?: 0) + (b ?: 0))
return total

 

Could you help me?

3 answers

1 accepted

0 votes
Answer accepted

If you want the "calculated" field to be always up to date (and not just updated during transitions), you should create a scripted field. This can be done in ScriptRunner or using the JMCF app (which makes it a little easier thanks to a simplified API).

is there any way to achieve this without having to buy Add-ons?

Thanks to community. 

Done by creating scripted field: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

Issue issue = issue
def id=issue.getId()

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def BI = customFieldManager.getCustomFieldObject(10508)
def Rec = customFieldManager.getCustomFieldObject(10509)
def Cus = customFieldManager.getCustomFieldObject(10510)
def Acc = customFieldManager.getCustomFieldObject(10511)
def Ret = customFieldManager.getCustomFieldObject(10512)


def a = Integer.parseInt((issue.getCustomFieldValue(BI) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(Rec) ?: 0).toString().replaceAll(~/[.].*/, ""))
def c = Integer.parseInt((issue.getCustomFieldValue(Cus) ?: 0).toString().replaceAll(~/[.].*/, ""))
def d = Integer.parseInt((issue.getCustomFieldValue(Acc) ?: 0).toString().replaceAll(~/[.].*/, ""))
def e = Integer.parseInt((issue.getCustomFieldValue(Ret) ?: 0).toString().replaceAll(~/[.].*/, ""))


def sum = (a ?: 0) + (b ?: 0) + (c ?: 0) + (d ?: 0) + (e ?: 0)
return sum

0 votes
Antoine Berry Community Leader Mar 10, 2020

Hi @Николай Киселев ,

Your code seems fine and you could update a field adding this snippet : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def fieldToUpdate = customFieldManager.getCustomFieldObject(10510)
def oldValue = issue.getCustomFieldValue(fieldToUpdate)

fieldToUpdate.updateValue(null, issue, new ModifiedValue(oldValue, total), new DefaultIssueChangeHolder())

Keep the reindex post-function after the script.

Antoine

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Apps for Confluence you won't want to miss: RSVP for September's Appy Hours

Calling all collaborators and Confluence users! Our Appy Hours event on September 29th features 4 presenters demoing functionality to superpower Confluence. Don't miss learning about these apps i...

117 views 0 9
Read article

Atlassian Community Events