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 votes

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,754 views 11 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot