You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
I'm trying to create a custom field that calculates the number of working days in the Sprint the current issue is in, but I'm a little stuck.
I'm on Jira 7.13 Server
I've created two custom fields to get a timestamp of the start/end of a Sprint:
Start date of a Sprint
End date of a Sprint
Here's the code:
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.time.DayOfWeek
// the name of the custom fields to compare
final String dateCustomFieldName = "Start date of a Sprint"
final String dateCustomFieldName2 = "End date of a Sprint"
def dateCustomField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(dateCustomFieldName)
def dateCustomField2 = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(dateCustomFieldName2)
if (!dateCustomField) {
log.info "Could not find custom field with name $dateCustomFieldName"
return null
}
if (!dateCustomField2) {
log.info "Could not find custom field with name $dateCustomFieldName2"
return null
}
if (!issue.getCustomFieldValue(dateCustomField) || !issue.getCustomFieldValue(dateCustomField2)) {
return null
}
def customFieldDate = (issue.getCustomFieldValue(dateCustomField) as Timestamp).toLocalDateTime().toLocalDate()
def customFieldDate2 = (issue.getCustomFieldValue(dateCustomField2) as Timestamp).toLocalDateTime().toLocalDate()
if (!customFieldDate2.isBefore(customFieldDate)) {
return null
}
def weekend = EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
def workingDays = 0
while (customFieldDate2.isBefore(customFieldDate)) {
if (!(customFieldDate2.dayOfWeek in weekend)) {
workingDays += 1
}
customFieldDate2 = customFieldDate2.plusDays(1)
}
workingDays
Here's the error I'm getting:
2019-09-20 10:44:38,325 ERROR [runner.ScriptFieldPreviewRunner]: ************************************************************************************* 2019-09-20 10:44:38,325 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Tue Sep 10 14:40:42 PDT 2019' with class 'java.lang.String' to class 'java.sql.Timestamp' at Script326.run(Script326.groovy:26)
Any help would be much appreciated!
Thanks,
Matt
I copied your script exactly into my environment and only changed the name of the 2 fields to use 2 of my date fields. It works just fine.
That error seems to imply that one of the 2 custom fields is not actually a "date picker" or "date time picker" field, but instead, a free text field populated with a text rendering of the date value.
If those fields are indeed text fields, then you need to add some code to parse that text into a date based on the localization used to generate the date string
Using a "Date picker" worked, but unfortunately, the two Fields are custom "Date Time" Fields and the code is not working.
These two fields are custom "Date Time" fields.
Start date of a Sprint
End date of a Sprint
Would it just be easier to get the Sprint start/end right in the cose instead of trying to get a custom field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are those two fields custom scripted fields?
If so, just change the "searcher" for those custom fields to "Date Time Range Picker"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
They are custom scripted fields, but there is no option to change the "searcher". Am I missing something? This is for a Server instance.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You have to do this from the Custom Field page in Jira. There is a shortcut from the scripted field list screen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, I've found it and changed them. But it's still giving me the error...
I appreciate your help so far! This has been a very tough one to crack for me.
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.