Error on scripted field (illegal "as" cast)

Itamar Ben Sinai August 15, 2016

Hi,

I have this script that returns an error in JIRA log and is referred to in Scriptrunner support (https://productsupport.adaptavist.com/browse/SRJIRA-1018)

Can anyone direct me to the fix needed in this script?

Thanks

 

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.FieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.core.util.DateUtils

  

def fieldManager = ComponentAccessor.getCustomFieldManager() as FieldManager

def startCF = fieldManager.getCustomField("Downtime Start") as CustomField

def endCF = fieldManager.getCustomField("Downtime End") as CustomField

def startTime = startCF.getValue(issue) as Date

def endTime = endCF.getValue(issue) as Date

if (startTime == null || endTime==null) {return null}

 

return (endTime-startTime) / (60 * 1000) as Double

2 answers

0 votes
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.
August 29, 2016

The same code doesn't produce any type checking errors for me - what version of the plugin are you using?

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

Try removing all the "as" statements.  You don't need them, as groovy handles the object types automatically.

Itamar Ben Sinai August 15, 2016

image2016-8-15 15:53:14.png

Itamar Ben Sinai August 15, 2016

image2016-8-15 15:54:6.png

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

Er, fieldManager doesn't have getCustomField as a function like that.

Try

def myCustomField = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("My Field")
Itamar Ben Sinai August 15, 2016

There is an improvement, Thanks

can you look at the next issue?

image2016-8-15 16:11:37.png

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2016

Those two values are dates, not numbers.  As humans, we can make the intuitive leap from "Sunday - Wednesday = 3 days", but computers can't.  You might as well be asking it to do "apple - cat = ???".  Convert the dates to epoch time or something like that (epoch time is something like "number of milliseconds since base date")

Suggest an answer

Log in or Sign up to answer