Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,359,975
Community Members
 
Community Events
168
Community Groups

Custom field in Jira that turns working days into a number

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

1 answer

0 votes

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?

Are those two fields custom scripted fields? 

If so, just change the "searcher" for those custom fields to "Date Time Range Picker"

They are custom scripted fields, but there is no option to change the "searcher". Am I missing something? This is for a Server instance.Capture.PNG

You have to do this from the Custom Field page in Jira. There is a shortcut from the scripted field list screen.

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

179 views 1 6
Read article

Atlassian Community Events