Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate sum of script field values

jypark May 29, 2019

Hi all,

 

I want to calculate the sum of progress (script field) in the board.

I have already made "Progress(script fields)".
How can I get the total of this?

Is it possible?

 

2 answers

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 30, 2019

In theory, you could use a script to get the values from scripted fields and add them up.

In reality, you must not do that on the same issue because the calculations are not done in the same order - a value that uses another scripted field may be being calculated before or after you have calculated the field it is relying on.

The other problem you have is that fields belong to issues, not boards.  So you cannot create a field like this, you would need to write code that amends the boards to include your field.

jypark June 13, 2019

Hi, @Nic Brough -Adaptavist- 

Sorry for my late reply.

 

How about utilize the "progress (script field)"?

ex) if Progress(Script Field) != null

          else sum


please check the script of another reply!

 

Thank you.

Regards,

Jiyeon Park.

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2019

No, do not do that.  That is exactly what I said you should not do.

You're tossing a coin as to which script runs first.  The script you've given here could return the wrong result if the progress field changes in the same change.

jypark June 15, 2019

oh. I understand. Thank you!

Can i try that in a way that doesn't reference other script fields?

ex) if Original Estimate(by board) != null

        else sum.total original estimate(by board)

      return total progress = (total original estimate / total original estimate) * 100

How about this?

however, i cannot create a script. I don't know the script.

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2019

Yes, that will work fine.  A more generic example would be

  • Field A - normal field
  • Field B - normal field
  • Field C - normal field
  • Field D - scripted field for A + B
  • Field E - scripted field for D + C

Field E will fail randomly when A or B are entered or edited, because D could be calculated before or after E.

So you change Field E so that it looks at what D should have in it - the code changes from D + C to (A + B) + C.

To do scripted fields, you need an app to provide them - Scriptrunnner is the one I know best (of course, as I work for Adaptavist)

jypark June 17, 2019

Thank you so much~!

I solved the problem using a "script runner"!

Can I ask you one more question?

 

I would like to do "SUM" only if the "original Estimate is not 0" and "issue status is done".

What code do I need to add if I want to find out that the issue is Done?

 

The code below is the code that I used to make "Total Progress"(Script Field).

import com.atlassian.greenhopper.service.PageRequests
import com.atlassian.greenhopper.service.rapid.SavedFilterService
import com.atlassian.greenhopper.service.rapid.view.RapidViewQuery
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser

log.warn("--- Start script -----------------------------------")

enableCache = {-> false}

def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def projectManager = ComponentAccessor.projectManager

def rapidViewService = PluginModuleCompilationCustomiser.getGreenHopperBean(RapidViewService)
def savedFilterService = PluginModuleCompilationCustomiser.getGreenHopperBean(SavedFilterService)

def project = projectManager.getProjectObj(issue.projectId)
def query = RapidViewQuery.builder().project(project)
//Get all the boards from the project
def rapidViews = rapidViewService.getRapidViews(user, PageRequests.all(), query.build())

def rapidViewList = rapidViews.get().values
log.warn("rapidViewList = $rapidViewList")

def issuesInBoards = new HashSet<Issue>()
rapidViewList.each {
def filter = savedFilterService.getSavedFilter(user, it.savedFilterId)
def searchResult = searchProvider.search(filter.get().query, user, PagerFilter.getUnlimitedFilter())
//Getting all the issues from all the boards (only the one with a grater than 0 estimate)
searchResult.getIssues().each { documentIssue ->
if(documentIssue.originalEstimate > 0){
issuesInBoards.add(documentIssue)
}
}
}

log.warn("issuesInBoards = $issuesInBoards")
//Calculate the total estimate for all the issues inside the boards
def totalEstimateForBoards = issuesInBoards.sum {
(it as Issue).originalEstimate
} as Integer

def issueEstimate = issue.originalEstimate
log.warn("issueEstimate = $issueEstimate")
log.warn("totalEstimateForBoards = $totalEstimateForBoards")
//Calculate the percentage between our estimate and the total
if(totalEstimateForBoards > 0){
return ((totalEstimateForBoards / totalEstimateForBoards) * 100) + "%"
}else{
return 0 + "%"
}

log.warn("------------------------------------------- End script ---")
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 23, 2019

issue.getStatus() should give you the status of the issue so you can test against it.

jypark July 24, 2019

I'm so late, sorry.

I added the code you talked about, but nothing happened.
Other scripts have not changed, but only this part has changed this way.

 

if(issueEstimate != null && issue.getStatus("Done") && totalEstimateForBoards > 0){
return ((issueEstimate / totalEstimateForBoards) * 100) + "%"
}else{
return 0 + "%"
}  

 

please, I hope you can help me a little more.

Thank you so much.

0 votes
Fabio Racobaldo _Catworkx_
Community Champion
May 30, 2019

Hi @jypark ,

beacuse of scripted fields are calculated on view (there are no values stored), you need to recalculate sum of all other progress fields into your "Total" scripted field.

Ciao,

Fabio

jypark June 13, 2019

Hi @Fabio Racobaldo _Catworkx_ 

Thank you for your reply.

 

if that is possible, how should I make script?

I need a "Total" scripted field are calculated by project(board).

can you make it?

 

I made it but it is not working and it is not working by board.
The script below is something I made.

//////

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
List<String> fieldNames = ["Progress"]
List<Integer> scores = []
for (String field : fieldNames){
def cf = customFieldManager.getCustomFieldObjectByName(field)
scores.add((Integer) issue.getCustomFieldValue(cf)?: 0)
}
return scores.sum {it}

//////

 

Thank you.

Regards,

jiyeon Park

Suggest an answer

Log in or Sign up to answer