I am getting this error:
groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1/2/1990' with class 'java.lang.String' to class 'java.util.Date'.
Kindly, help me over come this error.
Is this for ScriptRunner JIRA Cloud or ScriptRunner for JIRA Server?
You're getting that error because you have a "Date" object and you are trying to subtract something that is not actually a "Date" object. Most likely, you need to cast the value you are trying to subtract as a "Date" object. See example script below:
import com.atlassian.jira.component.ComponentAccessor;import com.atlassian.jira.issue.Issue;import java.util.Date.*def customFieldManager = ComponentAccessor.getCustomFieldManager();def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601')def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_11602')if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {def dateValue = issue.getCustomFieldValue(dateFieldObject) as Datedef dateValue2 = issue.getCustomFieldValue(dateFieldObject2) as Datereturn dateValue - dateValue2}
Hi, this is for ScripRunner JIRA Server.
What I am trying to do here is, from a custom field value in this case date, subtract the year value and show the resulting date as result of another custom field.
Thank you, I was able to over come the Groovy Cast Exception. I edited the code to what I was trying to achieve, though I am getting a java.lang.NullPointerException for this code:
def customFieldManager = ComponentAccessor.getCustomFieldManager();def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601')def dateValue = issue.getCustomFieldValue(dateFieldObject) as DatedateValue.getYear()
Hi George,
Where are you using this script? In a Scripted Field, post-function, etc?
Looks like you are also missing an import for ComponentAccessor.
import com.atlassian.jira.component.ComponentAccessordef customFieldManager = ComponentAccessor.getCustomFieldManager()def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601')def dateValue = issue.getCustomFieldValue(dateFieldObject) as DatedateValue.getYear()
Hi Joshua,
I am coding this in Scripted Field.
I do have the import com.atlassian.jira.component.ComponentAccessor
It still gives the same error.
The dateValue is supposed to contain : 1/4/2000 (format: dd/mm/yyyy)
But instead when I check to see if it s returning the above date, before I do the dateValue.getYear(), it gives me the error java.lang.NullPointerException
I am guessing this is because the code did not read the dateValue from my JIRA issue.
on the other hand when i use this code:
I am getting this error for the below code:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1/4/2000' with class 'java.lang.String' to class 'java.util.Date'
def customFieldManager = ComponentAccessor.getCustomFieldManager();def dateField = customFieldManager.getCustomFieldObjectByName('customfield_11')
if(issue.getCustomFieldValue(dateField)) {def dateValue = issue.getCustomFieldValue(dateField) as Datereturn dateValue.getYear()}
-- When I remove the 'as date' return type from def def dateValue = issue.getCustomFieldValue(dateField), this is the error I get :
groovy.lang.MissingMethodException: No signature of method: java.lang.String.getYear() is applicable for argument types: () values: [] Possible solutions: getChars(), getChars(int, int, [C, int), getAt(groovy.lang.Range), getAt(groovy.lang.IntRange), getAt(groovy.lang.Range), getAt(int)
def customFieldManager = ComponentAccessor.getCustomFieldManager();def dateField = customFieldManager.getCustomFieldObjectByName('customeField_11')
if(issue.getCustomFieldValue(dateField)) {def dateValue = issue.getCustomFieldValue(dateField)return dateValue.getYear()}
It looks like you're new here. Sign in or register to get started.