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
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 @PD Sheehan
Getting error on line
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @PD Sheehan ,
While Preview or Running Getting this error ,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.