Hi All,
Please can you advise:
I'm trying to create a script runner post-function to add one year to a field.
Have the below,
------------------------------------------------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// the dependent custom field
def dateACF = customFieldManager.getCustomFieldObjectByName("Forecast Date")
// get the value of the date A custom field
def dateAValue = issue.getCustomFieldValue(dateACF) as Date
// build the new date
def dateB = Calendar.getInstance()
// copy to the new date the value of the Date A custom field
dateB.setTime(dateAValue)
// add one year
dateB.add(Calendar.YEAR, 1)
//write back to custom field
issue.setCustomFieldValue(dateACF,dateB)
------------------------------------------------------------
This appears to validate OK in the Scriptrunner add on but when i try to execute the workflow transition i get the below error:
"Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB java.util.GregorianCalendar cannot be cast to java.util.Date"
Any advice would be appreciated.
Tom
Thank you so much Tuncay. You are amazing! I've been struggling with this for a fortnight.
I can't believe someone on the Alsatian forum answered one of my questions with a succinct, rational answer and not developerspeak. I'm so Happy!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm glad that it helped. Thanks for your kind words.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For the benefit of anyone else who is struggling with this and trawling the internet with despair;
------------------------------------------
//The below ScriptRunner post-function adds a specific number of days e.g. 365 to a custom field.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//define the dependent custom field
def dateACF = customFieldManager.getCustomFieldObjectByName("CustomFieldName")
// get the value of the date A custom field
def dateAValue = issue.getCustomFieldValue(dateACF) as Date
// build the new date
def dateB = dateAValue.plus(365)
//write back to custom field
issue.setCustomFieldValue(dateACF,dateB)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Or in your code you should convert GregorianCalendar to date
issue.setCustomFieldValue(dateACF,dateB.toInstant())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, Sorry tried this one but does not work.
Using the code:
-------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// the dependent custom field
def dateACF = customFieldManager.getCustomFieldObjectByName("Forecast Date")
// get the value of the date A custom field
def dateAValue = issue.getCustomFieldValue(dateACF) as Date
// build the new date
def dateB = Calendar.getInstance()
// copy to the new date the value of the Date A custom field
dateB.setTime(dateAValue)
// add one year
dateB.add(Calendar.YEAR, 1)
// convert variable to correct type + write back to custom field
issue.setCustomFieldValue(dateACF,dateB.toInstant())
---------------
Generates the below error on the workflow transition:
http://confluence.atlassian.com/x/3McB java.time.Instant cannot be cast to java.util.Date
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My bad, I think this should work
issue.setCustomFieldValue(dateACF, new Date(dateB.getTime()))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.