Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Custom field in Jira that turns working days into a number

Matthew_Schulz September 20, 2019

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

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2019

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

Matthew_Schulz September 23, 2019

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?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2019

Are those two fields custom scripted fields? 

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

Matthew_Schulz September 23, 2019

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

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2019

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

Matthew_Schulz September 23, 2019

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.

TAGS
AUG Leaders

Atlassian Community Events