Hi,
I tried this script in a groovy script field :
FormField ITCost = getFieldByName ("IT Cost (T&M)")
FormField OPSCost = getFieldByName ("Total OPS Number of MD Time & Material")
def ITCV = ITCost.getFormValue() ?: 0
def OPSCV = OPSCost.getFormValue() ?: 0
def ITOPSVSUM = ITCV + OPSCV
but I got this error message :
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5.groovy: 1: unable to resolve class FormField @ line 1, column 11. FormField ITCost = getFieldByName ("IT Cost (T&M)") ^ Script5.groovy: 2: unable to resolve class FormField @ line 2, column 11. FormField OPSCost = getFieldByName ("Total OPS Number of MD Time & Material") ^ 2 errors
Any ideas ?
Thanks in advance
Hi Mel,
I can confirm that if a value is null then it will not be able to be used in a calculation.
You could use an if else statement to check if one of the values is null and only do the calculation if both fields contain values.
An example of what this might look like using Nic's code above is below.
// Get values from 2 custom fields def valA = getCustomFieldValue("A") as Double def valB = getCustomFieldValue("B") as Double // check both fields contain valid numeric values if (valA != null && valB != null){ return valA + valB }else{ // return to some code to indicate a null value in one of the fields return "-1" }
I hope this helps
Kristian
Hi,
Try this simple code in field C
def aval = getCustomFieldValue("A") def bval = getCustomFieldValue("B") return aval+bval
Assuming A,B,C are number fields
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use def only when you don't know the type, else use int/double accordingly
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi thanks to you and phill. by Try this simple code in field C, you mean in the description or in Inline scripted field tab via Admin ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are you importing your classes in advance of this section of script? If not this could be the cause of your error.
Eg
import com.atlassian.jira.component.ComponentAccessor
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I agree with Phil.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm trying to script a validation on summing just 2nd value from multiple cascading fields. Does anyone know if this is possible?
cfValues["CascadingSelect 1"]?.values()*.value == ["AAA", "a1"]
cfValues["CascadingSelect 2"]?.values()*.value == ["BBB", "b1"]
a1 + b1 == 100
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is there a way to do this with time fields? I was able to use this same code, however I get a result of 0, and I'm thinking it's b/c my custom fields are time/date fields.
I am attempting to calculate the total duration between two custom date fields using:
import com.atlassian.jira.component.ComponentAccessor
return getCustomFieldValue("Outage End") - getCustomFieldValue("Outage Start")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't think the fields will be returning objects you can perform arithmetic on.
I seem to remember that they'll give you date/time stamps, which you could convert to "epoch" time, which is a number of (milli)seconds since a base date. Then subtract them, and convert the resulting number of (milli)seconds back up to minutes/hours/years/etc
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.
Hi Mel, I am glad the solution works. Thank you for accepting the answer. Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Kristian Walker (Adaptavist)
Sorry I get back to this request because finally some of my number fields can be null. Is there a solution to integrate a value= null into the formula? if not would you have an idea to be able to add several number fields, assuming that one or several of them can be null?
thanks in advance for your help
Melissa
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You would just need to check in the if statement for if value == null. You will not be able to add a null value to a number. What you would need to do is have a separate if statement for each field before the calculation which adds the values up and if it any field is null set its value to 0 so that it can be used in the calculation.
I hope this helps
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks @Kristian Walker (Adaptavist). I tried this code but it does not work and I get a message telling me that the null values are not supported by this method. Do i have to write this stateent in the description of the field ?
import com.atlassian.jira.component.ComponentAccessor
def aval = getCustomFieldValue("Accounting Quality & Integrity Control Cost (T&M)") def bval = getCustomFieldValue("Bonds Cost (T&M)") def cval = getCustomFieldValue("Collateral/Billing Cost (T&M)") def dval = getCustomFieldValue("Corporate Actions Cost (T&M)") def eval = getCustomFieldValue("Equities Cost (T&M)") def fval = getCustomFieldValue("Primary Issuance Cost (T&M)") def gval = getCustomFieldValue("Reference Data Cost (T&M)") def hval = getCustomFieldValue("IT Cost (T&M)")
// check both fields contain valid numeric values
if (aval == null) return aval("0")
if (bval == null) return bval("0") if (cval == null) return cval("0") if (dval == null) return dval("0") if (eval == null) return eval("0") if (fval == null) return fval("0") if (gval == null) return gval("0") if (hval == null) return hval("0")
if (aval != null && bval != null && cval != null && dval != null && eval != null && fval != null && gval != null && hval != null){ return aval+bval+cval+dval+eval+fval+gval+hval
}else{
// return to some code to indicate a null value in one of the fields
return "0"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mel,
If you use something similar to the code below then it will set the value to 0 if the field is null. You can add multiple fields in by getting each field and adding an if block for each field to set it 0 if its null.
def aval = getCustomFieldValue("A") def bval = getCustomFieldValue("B") // check both fields contain valid numeric values and if not set them to 0 if (aval == null) { aval = 0 } if (bval == null) { bval = 0 } if (aval != null && bval !=null){ return aval + bval }else{ // return to some code to indicate a null value in one of the fields return "0" }
I hope this helps.
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kristian,
it's great
! thanks you helped me a lot. It works.
sorry I don't have any programming skils so I don't know specific formula to build a code!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think you're confusing a script with a behaviour. Formfield is not a script object, you'd actually want to use custom field objects. Or, as it's groovy, the easier option of just "def something = x".
In fact, you should just enter
return getCustomFieldValue("A") + getCustomFieldValue("B")
in the scripted field and set the output type to number
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oops. Chander beat me to it! Sorry!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You pretty much had it in your question to be honest. But I'd do something like def returnval = 0 def myA = getCustomFieldValue("A") ?:0 def myB = getCustomFieldValue("B") ?:0 return (myA + myB)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bother, scraped the wrong chunk of script. Lose the first line of that!
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.