Behaviours Plugin: Groovy Script to add the values of two Number Fields

Uday Karthik Yadlapalli January 27, 2015

Hello,

I am trying to write a Groovy Script in Behaviours Plugin.
I have two Database Custom Fields,  "Header Priority Value" and "SubHeader Priority Value".
I get the values 3.000000 and 2.000000 for these fields (Using SQL Query).
I have to add the values in those two fields and get the sum, and use that sum to set Priority.

This is the script i put on "SubHeader Priority Value" field:

FormField GV = getFieldById ("priority")
FormField HP = getFieldByName ("Header Priority Value")
FormField SHP = getFieldByName ("SubHeader Priority Value")
String HPV = HP.getValue()
String SHPV = SHP.getValue()
String PVSUM = HPV + SHPV
log.error PVSUM

When i run this i am getting 3.0000002.000000 instead of 5.000000
I think this is because the values are considered as Strings. How do i do it ? 

1 answer

1 accepted

0 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 27, 2015

you're casting them to strings... try using

def HPV = HP.getValue() 

etc

if that doesn't work try casting to longs:

def HPV = HP.getValue() as Long

If they could be null try this:

def HPV = HP.getFormValue() ?: 0
def SHPV = SHP.getFormValue() ?: 0
def PVSUM = HPV + SHPV

 

 


Uday Karthik Yadlapalli January 27, 2015

I tried 'def' but got an error. I will try to Cast it as Long and see if it works. Thank You.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 27, 2015

I don't see how you can have got an error... paste the full minimal code and include the error.

Uday Karthik Yadlapalli January 28, 2015

Right now i have solved the problem by using only one field "SubHeader Priority Value" to determine the "Priority". But, i want to know how to add Numbers. So, This is the code i used: FormField GV = getFieldById ("priority") FormField HP = getFieldByName ("Header Priority Value") FormField SHP = getFieldByName ("SubHeader Priority Value") def HPV = HP.getValue() def SHPV = SHP.getValue() def PVSUM = HPV + SHPV log.error PVSUM And i got this: http-bio-8080-exec-13 ERROR uyadlapalli 801x8141x6 1bvbulw /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [onresolve.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: uyadlapalli, fieldId: customfield_11703, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.util.json.JSONObject$Null.plus() is applicable for argument types: (com.atlassian.jira.util.json.JSONObject$Null) values: [null] Possible solutions: split(groovy.lang.Closure), is(java.lang.Object), use([Ljava.lang.Object;), wait(), grep(), find() at Script1.run(Script1.groovy:8)

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 28, 2015

looks like one or both is null, check my updated answer

Uday Karthik Yadlapalli January 28, 2015

Yes Jamie. They will be Null initially. Thank you. That Works.

Suggest an answer

Log in or Sign up to answer