Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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.
Sep 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

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.
Sep 23, 2019

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

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.
Sep 23, 2019

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.

TAGS
AUG Leaders

Atlassian Community Events