Forums

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

Calculate sum of customfield of one issue type using Scriptrunner

Christian_Grüner June 3, 2020

Hey guys,

 

i am not SW developer so please excuse any beginner questions.

 

I am trying to calculate the sum of a specific custom field for all issues of one type.

My understanding is that If you use scripts inside the listener the scripts are always run for a one issue. This means that I can only access the values of a specific custom field per issue.

How do I manage to access all my project issues inside a listener script?
(it is fine it the script is run each time one of my project issues is changed)

 

Best regards

CG

 

2 answers

1 accepted

0 votes
Answer accepted
saulo fonseca June 9, 2020

Hi

saulo fonseca June 9, 2020

Hi @Christian_Grüner 

See:

Issue1 - Custom Field1 = 2
Issue2 - Custom Fied2 = 3
Issue3 - Custom Field3 = 2

The sum is 7.

If my understanding is correct, then the code below can help you See:


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

def customField = ComponentAccessor.getComponent(CustomFieldManager)
def cf = customField.getCustomFieldObjectByName('Name of your Custom Field')
def issueMgr = ComponentAccessor.getIssueManager()

def listIssues = issueMgr.getIssueObjects(issueMgr.getIssueIdsForProject(Your Project Id))
def sum = 0

listIssues.each { elem ->

 if (elem.getIssueType().getName() == 'Name of your Reques type' && elem.getCustomFieldValue(cf) != null){

              sum += elem.getCustomFieldValue(cf)

        }

}

Could you answer a question?
1) Shouldn't you choose besides "Issue Updated" the option "Issue Created" too? Because, a new issues will have
the custom field with value, will not?

You can use the code in "Script Console" (Jira Administration -> Manage Apps -> ScriptRunner -> Console)

I look forward to your reply

Saulo Martins
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 9, 2020

Hi @Christian_Grüner 

See:

Issue1 - Custom Field1 = 2
Issue2 - Custom Fied2 = 3
Issue3 - Custom Field3 = 2

The sum is 7.

If my understanding is correct, then the code below can help you See:


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

def customField = ComponentAccessor.getComponent(CustomFieldManager)
def cf = customField.getCustomFieldObjectByName('Name of your Custom Field')
def issueMgr = ComponentAccessor.getIssueManager()

def listIssues = issueMgr.getIssueObjects(issueMgr.getIssueIdsForProject(Your Project Id))
def sum = 0

listIssues.each { elem ->

if (elem.getIssueType().getName() == 'Name of your Reques type' && elem.getCustomFieldValue(cf) != null){

sum += elem.getCustomFieldValue(cf)

}

}

Could you answer a question?
1) Shouldn't you choose besides "Issue Updated" the option "Issue Created" too? Because, a new issues will have
the custom field with value, will not?

You can use the code in "Script Console" (Jira Administration -> Manage Apps -> ScriptRunner -> Console)

I look forward to your reply

0 votes
saulo fonseca June 9, 2020

Hi

See:

Issue1 - Custom Field1 = 2
Issue2 - Custom Fied2 = 3
Issue3 - Custom Field3 = 2

The sum is 7.

If my understanding is correct, then the code below can help you See:


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

def customField = ComponentAccessor.getComponent(CustomFieldManager)
def cf = customField.getCustomFieldObjectByName('Name of your Custom Field')
def issueMgr = ComponentAccessor.getIssueManager()

def listIssues = issueMgr.getIssueObjects(issueMgr.getIssueIdsForProject(Your Project Id))
def sum = 0

listIssues.each { elem ->

if (elem.getIssueType().getName() == 'Name of your Reques type' && elem.getCustomFieldValue(cf) != null){

sum += elem.getCustomFieldValue(cf)

}

}

Could you answer a question?
1) Shouldn't you choose besides "Issue Updated" the option "Issue Created" too? Because, a new issues will have
the custom field with value, will not?

You can use the code in "Script Console" (Jira Administration -> Manage Apps -> ScriptRunner -> Console)

I look forward to your reply

Suggest an answer

Log in or Sign up to answer