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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,769
Community Members
 
Community Events
176
Community Groups

Calculate custom field from child to parent

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

0 votes

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.

Hi @Peter-Dave Sheehan 

Getting error on line 

 it.getCustomFieldValue(cf)
}
error.PNG
Please help  and let me know what is "it." here

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.

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 

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
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events