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

Iram Masood August 14, 2020

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

Comment

Log in or Sign up to comment
Iram Masood August 17, 2020

Can Anyone help here please.

TAGS
AUG Leaders

Atlassian Community Events