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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,556,177
Community Members
 
Community Events
184
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
David Fischer _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Mar 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).

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

Atlassian Community Events