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,296,856
Community Members
 
Community Events
165
Community Groups

I want to find the difference in number of days between baseline end date and end date

Edited

For this i  am using the below code but it is showing an error:-

 

Code:-

import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.time.*
import java.time.format.*

// The issue key
final issueKey = 'IT-3'
//def cField = customFieldManager.getCustomFieldObject("customfield_10304")
//final dateFieldName = 'Baseline end date'
//def dateFieldName = issue.getCustomFieldValue("Baseline end date")
def dateFieldName = getFieldIdFromName('Baseline end date')
final chronoUnit = ChronoUnit.DAYS

// Jira datetime field format
String dateTime = "2012-02-22T02:06:58.147Z";
ZonedDateTime d = ZonedDateTime.parse(dateTime);
//final String dateTime = "2012-02-22T02:06:58.147Z"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SX")
//final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter)
final DateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME

// Pick a date that you would like to calculate from
def issueFieldId = getFieldIdFromName(dateFieldName)
// Pick a date from another date field, in this case the built-in 'Updated' field
def updatedFieldId = getFieldIdFromName('End date')
//def updatedFieldId = 'End date'

def createdDate = ZonedDateTime.parse(getIssueField(issueKey, issueFieldId), formatter)
def updatedDate = ZonedDateTime.parse(getIssueField(issueKey, updatedFieldId), formatter)

def dateDifference = chronoUnit.between(createdDate, updatedDate)

/**
* Get the issue field data on a given issue
* @Param issueKey The key of the issue to get the data from
* @Param fieldId The field to get the data of
* @return The value of the field
*/
String 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
}

logger.warn "Failed to find issue: Status: ${result.status} ${result.body}"
null
}

/**
* Get the id of a field
* @Param fieldName The name of the field
* @return The field id
*/
String getFieldIdFromName(String fieldName) {
def fields = get("/rest/api/2/field").asObject(List).body
def customFieldObject = (fields as List<Map>).find { Map field ->
field.name == fieldName
}
(customFieldObject as Map).Id
}

// Return the value
"${dateDifference} ${chronoUnit.toString().toLowerCase()}"

Error is:-

java.lang.NullPointerException: Cannot get property 'Id' on null object at Script1.getFieldIdFromName(Script1.groovy:69) at Script1$getFieldIdFromName$0.callCurrent(Unknown Source) at Script1.run(Script1.groovy:24) at Script1$run.call(Unknown Source) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:39) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:29) at ConsoleScriptExecution1_groovyProxy.run(Unknown Source)

 

 

 

1 comment

Can Anyone help here please.

Comment

Log in or Sign up to comment
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

331 views 0 10
Join discussion

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