How to sum custom field values

Николай Киселев March 10, 2020

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
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.
March 10, 2020

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).

Om Joshi July 29, 2022

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

0 votes
Николай Киселев March 12, 2020

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 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
AUG Leaders

Atlassian Community Events