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

Calculate custom field from child to parent

Priti Sonar July 8, 2022

Hello ,

I m  having a complex requirement.

I m having one main issue Type Project  .

This Project  can have multiple  other issue Types let suppose Booking through issue links name  " Booking  Child".

such that all Booking tickets have a custom field , let suppose A (number Field).

Now I want in Project i.e main issue type have a  custom field total of A = sum of all A of all linked Booking Ticket.

Total A will be scripted field .

But i m not Sure from where to start .If at all it has been task-subtask it will be fine.

But now it is Linking Issue types .

Could you please provide me with a script where i could Achieve this 

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2022

You can use the IssueLinkManager to get all the issues from a specific issue link type

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


def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLinkManager = ComponentAccessor.issueLinkManager
def nameCustomFieldA = 'number field A'
def
cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(nameCustomFieldA)
issueLinkManager.getLinkCollection(issue, currentUser).getOutwardIssues('Booking').sum{
it.getCustomFieldValue(cf)
}

Depending on your link configuration, you may need to use the getInwardIssues instead. And adjust for the correct link type.

Priti Sonar July 11, 2022

Hi @Peter-Dave Sheehan 

Getting error on line 

 it.getCustomFieldValue(cf)
}
error.PNG
Please help  and let me know what is "it." here
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2022

In groovy, when you enter a "closure", a variable called "it" is automatically assigned to the object that the closure is acting on.

In this case, the closure is one provided by groovy collection which includes generating a sum

Basically, with a list/collection, some of the most frequently used methods are going to be "each", "collect", "find" and "findAll"

And while the following works:

def list = [1,2,3,4]
def totalOfDouble = 0
list.each{ total = total+(it*2)}
assert total == 20

This is much shorter and acheives the same thing:

def list = [1,2,3,4]
def total = list.sum{it*2

Now you ask what "it" is... like I started to say, "it' is a placeholder

The following are all equivalent

list.sum{ it*2 }
list.sum{ it -> it*2 }
list.sum{ x -> x*2 }

Where x can be any variable name you choose (not already used in your script

As for the error, it is beucase I made a mistake.

The method getCustomFieldObjectsByName() can return more than 1 custom field. So we have to tell it that we expect only 1 and get that one out of the list (a list of size 1).

Like this:

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(nameCustomFieldA)[0]

You might still get a Static Type checking error. Those are not always bad. It's a way for the code syntax checker to tell you that it can't determine the outcome with 100% probability.

If you still get an error and you want it to go away, you can try to change 

it.getCustomFieldValue(cf)

To

it.getCustomFieldValue(cf) as Number

This will tell the syntax checker that we expect a number in the custom field and it will know that the sum operation will not likely fail.
But if you get an error on that line and don't fix it, the code will still run. Because when it extracts the value from the field. It WILL be a number and that will execute just fine.

Priti Sonar July 12, 2022

Hi @Peter-Dave Sheehan ,

While Preview or Running Getting  this error ,

java.lang.NullPointerException: Cannot invoke method sum() on null object at Script2136.run(Script2136.groovy:9)
An error occurred, the field will have no value.
My code is :
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue


def currentUser1 = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLinkManager = ComponentAccessor.issueLinkManager
def nameCustomFieldA = 'Booking Value (Billing Currency)'
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(nameCustomFieldA)[0]
issueLinkManager.getLinkCollection(issue, currentUser1).getInwardIssues('Booking Child').sum{
    it.getCustomFieldValue(cf)
}
The Field Should have the value 
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 12, 2022

Pehaps start with:

 

issueLinkManager.getLinkCollection(issue, currentUser1).getInwardIssues('Booking Child').collect{it.key}

 This should get you your list of issues.

If you don't get any issues, try to play with different variations of inwardIssues or outwardIssues and different issue type description. 

Then, the next step is to add logs:

issueLinkManager.getLinkCollection(issue, currentUser1).getInwardIssues('Booking Child').each{
def fieldValue = issue.getCustomFieldValue(cf)
log.info "$issue.key identified with value '$value' in $cf.name"
}

If there is a chance you get some empty values:

issueLinkManager.getLinkCollection(issue, currentUser1).getInwardIssues('Booking Child').sum{
def fieldValue = issue.getCustomFieldValue(cf)
fieldValue ?: 0 //return 0 if the fieldValue is empty/null
}
TAGS
AUG Leaders

Atlassian Community Events