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

ScriptRunner - find number of days between two custom date fields

Can anyone provide scriptrunner cloud, workflow post function, run script code to calculate the number of days between two custom field dates. 

I've seen the code below but the first two lines give an error...

Thanks.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import  java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_15968');

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_15967');

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {

   def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
   def dateValue2 = issue.getCustomFieldValue(dateFieldObject2) as Date
   def calculation = (dateValue - dateValue2)
    
   return (calculation as String)
}

1 answer

Hi Neil, 

The script you have over there is for ScriptRunner for Server, you can find a Cloud version of that script at Script Library - Calculate the difference between two dates

Hope that helps 

Thanos

Thanks Thanos. Yes, I assumed this was the Server version.

I'm not a coding expert but he code below appears to indicate you have to hard-code the issuekey.

How would I amend the code so, within a workflow (post function), the code returns the difference in days between two custom fields ("Analysis Date" and "Ready Date") for the issuekey being transitioned in the workflow?

What parts of the code below would I have to change?

Thanks for your help.

Neil

 

def issueKey = 'TEST-1'
//Pick a date that you would like to calculate from
def issueFieldId = getFieldIdFromName('Reqs Agreed Date')
//You can even use built-in field ids, like resolutiondate
resolutionFieldId = 'resolutiondate'

def reqagreeddate = new Date().parse('yyyy-MM-dd', getIssueField(issueKey, issueFieldId))
def resolutionDate = new Date().parse('yyyy-MM-dd', getIssueField(issueKey, resolutionFieldId))
def dateDifference = resolutionDate - reqagreeddate

return dateDifference + ' Days'

def getIssueField(issueKey, fieldId) {
def issueFieldValue = null
def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status == 200) {
result.body.fields.each { key, value ->
if (key == fieldId) {
issueFieldValue = value.toString()
}
}
logger.warn issueFieldValue
return issueFieldValue
} else {
logger.warn "Failed to find issue: Status: ${result.status} ${result.body}"
return null
}
}

def getFieldIdFromName(fieldName) {
def customFieldObject = get('/rest/api/2/field').asObject(List).body.find { it.name == fieldName
}
return customFieldObject.id
}

I would like the result (dateDifference) to be stored in custom field "Cycle Time")

I would like the result (dateDifference) to be stored in custom field "Cycle Time" so it can be queried with JQL.

The code copied from the adaptavist link provided doesn't compile. It throws up lots of errors when pasted into the "Script Context"

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

36 views 0 2
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