Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

behaviour sum of two number fields

Omprakash Thamsetty
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.
March 21, 2019

Hi,

 

I am doing sum of 2 number fields and then adding to original estimate.

Here is steps


1. Created 2 number fields ( numField1 and numField2 )

2. writing below code in behavior on numField1. When ever it get change then take the value and add with numField2 and then set to original estimate

def numField1 = getFieldById(getFieldChanged())

def numField1_Val = numField1.getValue()

def numField2 = getFieldById("customfield_17xxx") // numField2
def numField2_Val = numField2.getValue()

long totalnumFields = numField1 + numField2

def orgEstimate = getFieldById("timetracking_originalestimate")
orgEstimate.setFormValue(totalnumFields)

but this does not work. Through error

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '66' with class 'java.lang.String' to class 'long'

Look like there is some conversion required to Int but not finding it. Can anyone has any idea.

 

1 answer

1 accepted

1 vote
Answer accepted
Mark Markov
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.
March 21, 2019

Hello @Omprakash Thamsetty 

Yes, getValue returns String object, you need to convert it to long, like this

def numField1 = getFieldById(getFieldChanged())

def numField1_Val = numField1.getValue()

def numField2 = getFieldById("customfield_17xxx") // numField2
def numField2_Val = numField2.getValue()

long totalnumFields = Long.parseLong(numField1) + Long.parseLong(numField2)

def orgEstimate = getFieldById("timetracking_originalestimate")
orgEstimate.setFormValue(totalnumFields)
Omprakash Thamsetty
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.
March 21, 2019

@Mark Markov  look like I needs to import some method it seems. Getting error for long.parseLong . can you suggest me which one will be import

Mark Markov
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.
March 21, 2019

You dont need to import basic java things.

There is an error, you try to convert form fields instead of values,

try to replace

long totalnumFields = Long.parseLong(numField1_Val) + Long.parseLong(numField2_Val)
Omprakash Thamsetty
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.
March 24, 2019

@Mark Markov  How to check for null value in behavior. Actually I am trying issue.timespent so its giving null when I create issue. Obviously it is null but how to check that and set zero in my code ? look like some below

TTSpentVal = issue.timespent

def TTSpentVal = 0

if(TTSpentVal){

     TTSpentVal1=issue.timespent as Long

}else{

 TTSpentVal1=0

}

 

but it is not checking value and setting zero. how to check value as null and set zero

Mark Markov
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.
March 25, 2019

Like this

 

def TTSpentVal = issue.timespent

if(!TTSpentVal){

  TTSpentVal=0L

}
Omprakash Thamsetty
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.
March 25, 2019

  It still not checking null. Getting below error

java.lang.NullPointerException: Cannot get property 'timeSpent' on null object

Mark Markov
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.
March 26, 2019

Behaviour doesn's have issue variable, try to use underlyingIssue instead

def TTSpentVal = underlyingIssue?.timespent

Suggest an answer

Log in or Sign up to answer