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
Community Members
Community Events
Community Groups

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


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



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')
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}"

* 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 -> == 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 at Script1$ Source) at at at Source)




1 comment

Can Anyone help here please.


Log in or Sign up to comment
Community showcase
Published in Trello

Friday!... but wait!!

It's Friday!!!  I know you want to run away but wait.... I'll tell you what I'm going to do to REST MY MIND for the weekend, in case it might help you: I REVIEW my pending things in...

59 views 1 5
Read article

Atlassian Community Events