Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Is it possible to return a Double from a Scripted Field, but only display the integer part?

Andrew Frayling
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.
April 1, 2015

Hi,

Hoping I'm missing something simple with this. I've got a scripted field which calculates the Age of a JIRA ticket in days based on the current time and created timestamp and I'd like to display this as a rounded integer, but the indexer insists on a Double being returned and displaying decimal places. The code is really simple:

def createTime = issue?.getCreated()?.getTime();
Date now = new Date()
def currentTime = now.getTime()
def createdAgo = (currentTime - createTime) / 86400000 // milliseconds in a day
if (createdAgo) {
    return createdAgo.toDouble().round(0)
} else {
    return 0
}

but this returns values such as 6.0, 11.0, 9.0, 56.0, etc. when I just want 6, 11, 9, 56, etc.

If I change the return statement to

return createdAgo.toDouble().round()

then it displays what I want, but I get the following error:

"The indexer for this field expects a java.lang.Double but the script returned a java.lang.Long - this will cause problems."

I've tried every combination of trunc(), round(),floor(), etc. that I can think of, but I always end up in the same place. I either return a Double, which displays decimal places, or I end up with a Long or Integer which doesn't display decimal places, but gives the above error and causes issues to fail to re-index when they're edited.

It needs to be a Number field rather than a String so people can search for Age > 10, Age <= 7, etc.

Am I missing something obvious?

 

Cheers.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Jozef Kotlár
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.
April 1, 2015

You have possibility of creating custom template using velocity numberTool variable. Something like

$numberTool.format('integer',$value)

Anyway, your expectation that Age would be automagically incremented is false. Scripted fields are calculated and stored into index only if issue is updated (or explicitly reindexed).

Hint: It's better to teach your users to use created < -10d or created <= -7d.

 

 

 

Andrew Frayling
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.
April 2, 2015

Thanks Jozef. But an issue is indexed when it's created so it always has an indexed created date? Are you saying: Date now = new Date() def currentTime = now.getTime() only returns the date that the issue was last re-indexed?

Andrew Frayling
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.
April 2, 2015

Ignore that comment, I understand what you mean.

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.
April 2, 2015

As Jozef says, script fields cannot be used for calculating a value where one of the inputs is "outside" the issue, eg current time. They will refresh when the issue is viewed, and the value is stored in the index only when the issue is updated.

TAGS
AUG Leaders

Atlassian Community Events