Hello i'm trying to figure out as I'm not familiar with groovy scripts nor script runner.
I have 3 custom numerical fields named:
"An.Total", "Eng.Total", "Ops.Total"
For each issue in my current project, I'd like to add all three values into one value on the same issue called "Total".
"An.Total" + "Eng.Total" + "Ops.Total" = "Total"
Anything would help,
Thank you
Hi!
You need a scripted field, with a numerical searcher and template.
For the code, use this:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObjectByName("An.Total")
def cf2 = customFieldManager.getCustomFieldObjectByName("Eng.Total")
def cf3 = customFieldManager.getCustomFieldObjectByName("Ops.Total")
def sum = issue.getCustomFieldValue(cf1)?:0 + issue.getCustomFieldValue(cf2)?:0 + issue.getCustomFieldValue(cf3)?:0
return sum
Make sure that your fields are numeric, else this could cause some null exceptions.
Hope this helped, if you need any more help, please do say, and if this helped, please accept and upvote my answer so that other users can benefit from this question.
Cheers!
Dyelamos
Hi Daniel,
Does this have to be placed into the "Inline Script" section. If so, i'm seeing an error on the addition operator "+".
Thanks,
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The error suggests one or more of the values coming back from the fields is not a number.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Nic,
I've checked all 3 field including "An.Total","Eng.Total","Ops.Total". When created I've made them all Numerical fields.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also, I noticed that sum is defined by
def sum = issue.getCustomFieldValue(cf1)?:0 + issue.getCustomFieldValue(cf2)?:0 + issue.getCustomFieldValue(cf3)?:0
and is then is
return sum.
I did create a custom field named "Total", do I want to replace "sum"with my custom field "Total" and also define it in the line before?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jeffrey!
Those errors are static type checking. You can disregard them unless they give problems in execution time.
Please just save your script as is. It should work.
Also before you try any scriptrunner scripting you should thoroughly read, at least the basic sections of our documentation.
If you check the "Concepts" section, you will see an explanation about static field errors and what they actually are.
Without reading the documentation you might experience problems that are easily solved.
Cheers
DYelamos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'd agree with Daniel here, the static type checking is worth a read if you're going to do a lot of scripting, or run into these sort of errors.
An experiment to help see what is happening might be to try this:
def v1 = issue.getCustomFieldValue(cf1)?:0
def v2 = issue.getCustomFieldValue(cf2)?:0
def v3 = issue.getCustomFieldValue(cf3)?:0
return "Values: " + v1 + " : " + v2 + " : " + v3
That should help you see where the static types matter, and confirm what the code is getting back from the issue.
To see what is being passed around internally, it might be worth seeing:
return v1.getClass().getSimpleName() + " : " + v1.getClass().getSimpleName() + " : " + v1.getClass().getSimpleName() + " : "
Although I suspect this sort of brute-force information dump won't be needed
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
I was able to finally receive a return value. The only issue is the value cf1 is only being returned and I'm receiving error now under
The values I have under this issue is 12 + 13 + 14
return sum I receive just the 12.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What I did to resolve this issue was since it didn't like summation operator define as
define sum = issue.getCustomFieldValue(cf1)?:0 + issue.getCustomFieldValue(cf2)?:0 + issue.getCustomFieldValue(cf3)?:0
I used the summation operation in the return clause and also returned the value as a double since it was complaining about that too and came up with this.
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObjectByName("An.Tot")
def cf2 = customFieldManager.getCustomFieldObjectByName("Eng.Tot")
def cf3 = customFieldManager.getCustomFieldObjectByName("Ops.Tot")
def sum1 = issue.getCustomFieldValue(cf1)?:0
def sum2 = issue.getCustomFieldValue(cf2)?:0
def sum3 = issue.getCustomFieldValue(cf3)?:0
return (sum1 + sum2 + sum3) as double
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.