Hi Guys,
I have two date fields Target Start & Target End of date type, I need to find the difference between these two dates & put the difference in third field Days which is of text type.
I am using below code to do that, however, its not working. Can someone please take a look if I am doing something wrong or missing something ?
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.time.temporal.ChronoUnit
def Days = getFieldById("customfield_xxxxxx")
// Get the required component
def customFieldManager = ComponentAccessor.customFieldManager
// The name of the lower date custom field
final String lowerDateCustomFieldName = "Target start"
// The name of the higher date custom field
final String higherDateCustomFieldName = "Target end"
// Get the custom field objects
def lowerDateCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.name == lowerDateCustomFieldName }
def higherDateCustomField = customFieldManager.getCustomFieldObjects(issue).find { it.name == higherDateCustomFieldName }
if (!lowerDateCustomField || !higherDateCustomField) {
log.info "Could not find one ore more of the provided custom fields"
return null
}
// Get the date values from both issues
def lowerDateValue = issue.getCustomFieldValue(lowerDateCustomField) as Timestamp
def higherDateValue = issue.getCustomFieldValue(higherDateCustomField) as Timestamp
// Transform both values to instants
def lowerDateInstant = lowerDateValue?.toInstant()
def higherDateInstant = higherDateValue?.toInstant()
// Change the chrono unit to obtain the difference in other time unit.
final chronoUnit = ChronoUnit.DAYS
// Calculate the difference between the lower and the higher date.
Days.setFormValue(lowerDateInstant && higherDateInstant ? chronoUnit.between(lowerDateInstant, higherDateInstant) : null)
Thanks in advance.