Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Sum of custom field values from subtasks

Helen Porter
Contributor
April 30, 2020 edited

Hi,

We need to show the message to the user at the transition in the task. The message should display the sum of the calculated field from each subtask. Also, if in one of the subtasks the field value is zero, then instead of the sum show, for example, the value "?".

Any ideas?

1 answer

0 votes
John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 30, 2020

Hi Helen,

If I understand you correctly, as the user transitions the card from one status to the other, you want to show the user the value of the custom field. Correct?

If so, you could place a transition screen on the transition that displays the value of the field and place a condition on the transition where the custom field is > 0.

Then create another transition to the same status with a condition where the custom field 0 or is empty. There would be no screen attached to this transition. 

Does that sound like what you are trying to accomplish?

Helen Porter
Contributor
May 5, 2020

Hi John,

As the user transitions the card from one status to the other, we want to show the user sum the values of the custom field from the subtasks.

Is it possible to calculate the sum of the values ​​of fields of type Calculated (scripted) Number Field in a sil script to show a message to the user?

John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 5, 2020

You would have to update the calculated field as soon as one of the values is updated. Then when you call the transition screen, it's simply displaying the value in the field, not calculating it on the fly. 

I don't have ScriptRunner or Server, but I assume you could use it to calculate the value. 

I would use Automation For Jira for my Cloud version. 

Helen Porter
Contributor
May 6, 2020 edited

I think there can be two options for solving the problem.

The first option is to use the sil-script to calculate the sum of the field values from the subtasks. Then on the transition show a message in which the received sum of values will be indicated.

The second option is to create a field with the type Calculated (scripted) Number Field, and configure it so that the sum of the field values is calculated in it. And, as you suggested, on the transition make a screen with this field.

But the problem is that for the first option it is not possible to calculate the sum of the field values. Perhaps because the field type is "Calculated (scripted) Number Field". Here we would like to know from those who worked with these fields and faced similar problems.

And in the second option, we need to use GroovyScript. I am not good at Groovy scripts.

I executed the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

issue.getSubTaskObjects().size()

CustomField total = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Total labor")
double totalSum = 0;
for(Issue subtask: issue.getSubTaskObjects()){
if(subtask.getCustomFieldValue(total) != null)
totalSum += subtask.getCustomFieldValue(total)
}


return totalSum

The script is working. But I do not know how to add a condition:

if in one of the subtasks the field value is zero, then instead of the sum show, for example, the value "?".

Helen Porter
Contributor
May 6, 2020

I think there can be two options for solving the problem.

The first option is to use the sil-script to calculate the sum of the field values from the subtasks. Then on the transition show a message in which the received sum of values will be indicated.

The second option is to create a field with the type Calculated (scripted) Number Field, and configure it so that the sum of the field values is calculated in it. And, as you suggested, on the transition make a screen with this field.

But the problem is that for the first option it is not possible to calculate the sum of the field values. Perhaps because the field type is "Calculated (scripted) Number Field". Here we would like to know from those who worked with these fields and faced similar problems.

And in the second option, we need to use GroovyScript. I am not good at Groovy scripts.

I used the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

//return number subtasks
issue.getSubTaskObjects().size()

CustomField total = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Total")
double totalSum = 0;
for(Issue subtask: issue.getSubTaskObjects()){
    if(subtask.getCustomFieldValue(total) != null)
        totalSum += subtask.getCustomFieldValue(total)
}

return totalSum

 

The script is working. But I do not know how to add a condition:

if in one of the subtasks the field value is zero, then instead of the sum show, for example, the value "?".

John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 6, 2020

Sorry, but I am not a Groovy expert either. That's why we switched to Automation for Jira. :-)

Nor have I used those types of fields. I just use a standard Number field. 

Helen Porter
Contributor
May 6, 2020

I understood. :-)
Well, we will look for a solution.

John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 6, 2020

I would start by contacting support for the vendor. 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Upcoming Jira Events