Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Bitbucket Pipelines

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

478 views 10 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you