ScriptRunner (Scripted Field) - Calculation using numeric values in custom fields

Hello - 

I'm attempting to write a script to perform a calculation that is very similar to this recipe provided by Adaptavist (https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html)

 

There is also a similar approach, but using sub-tasks (https://answers.atlassian.com/questions/45972436) which does not apply to my case.

----------------

I have two issue types (Parent A & Child B), that have a one to many relationship and are related via issue links.

The objective is to aggregate the values for a specific custom field type from all the linked B type issues and subtract them from the same custom field on A.

The recipe from the documentation uses a specific method which relies on the "ComponentAccessor.getIssueLinkManager()" and I understand that to access custom fields we have to use the ComponentAccessor.getCustomFieldManager().

Any pointers or ideas on how to write this script as I to no avail am able to devise the correct syntax and methods to perform calculations with values stored in custom fields

Appreciate the help and support! THANKS

 

 

1 answer

0 votes
Vasiliy Zverev Community Champion Jan 09, 2017

Here is a draft for your case. It is better to use some listener to update values

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.link.IssueLink

Issue linkedIssue;

CustomField field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName");
double agregatedValue = 0;
for(IssueLink link: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.id)){
    linkedIssue = link.getDestinationObject();
    
    if(!linkedIssue.getIssueTypeObject().getName().equals("Type B"))
        continue;
    
    agregatedValue += linkedIssue.getCustomFieldValue(field) == null ? 0 : linkedIssue.getCustomFieldValue(field);
}

issue.setCustomFieldValue(field, agregatedValue)

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , issue
        , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()
)

Thank you for your quick reply Vasiliy.

I am not really a developer, and this is something that will really help  me out... so I appreciate the patience and help smile Putting a bit more detail as I tried to customize the provided template and ran into a few problems which I will describe.

 

To put better context, the issue types are "Purchase Order" and "Invoice".

As far as the issue linking goes, a "Purchase Order" issue can be linked to one or many "Invoice" issues.

 

The custom numeric field I want to perform calculations with is called "Order Value" (caveat, it is the same field for both issue types.)

 

The outcome I seek is to have 2 new scripted fields, "Invoiced Amount", and "Available Amount"

(I need more than these but figure that if I the idea for one, may be able to recreate the others.)

 

For the "Purchase Order" (parent) issue type:

  1. The field "Invoiced Amount" will be the aggregate SUM of the field "Order Value" of all the linked "Invoice" issues.
  2. The field "Available Amount" will be the difference between the "Order Value" (of the "Purchase Order"  parent issue) and the "Invoiced Amount"

 

Upon trying the script, this part was not yielding the list of linked issues.

for(IssueLink link: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.id)){
    linkedIssue = link.getDestinationObject();
     
    if(!linkedIssue.getIssueTypeObject().getName().equals("Invoice"))
        continue;

 

The calculation portion was also giving me an error, about not recognizing the method... 

agregatedValue += linkedIssue.getCustomFieldValue(field) == null ? 0 : linkedIssue.getCustomFieldValue(field);
}

 

This part of the script I removed as it updated the values of the fields in the "Purchase Order" (Type A) issue... the idea is that the value is stored in the new Scripted Field ("Invoiced Amount")

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , issue
        , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()
)

 

Perhaps you may be able to steer me in the right direction based on this additional information.
AGAIN... many many thanks in advance. Please let me know if this makes sense

Vasiliy Zverev Community Champion Jan 11, 2017

This part:

for(IssueLink link: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.id)){
    linkedIssue = link.getDestinationObject();
      
    if(!linkedIssue.getIssueTypeObject().getName().equals("Invoice"))
        continue;

should skip all issues not of invoice type.

Error for this

agregatedValue += linkedIssue.getCustomFieldValue(field) == null ? 0 : linkedIssue.getCustomFieldValue(field);

could be ignored.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,137 views 0 8
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you