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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

Suggest an answer

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

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

284 views 2 5
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you