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

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 vote

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

 

 


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

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

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)

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

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

101 views 0 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you