Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Find difference between two date fields & copy difference in third field

Vishal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 10, 2022

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.

2 answers

1 accepted

0 votes
Answer accepted
Vishal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2022

Those looking for the answer, there is nothing wrong with the script, the issue was with scriptrunner, it had a bug that it was not able to set the value of field using setFormValue(""),

scriptrunner version upgrade had a fix for it, which solved my issue. Here is release information if anyone looking for it https://docs.adaptavist.com/sr4js/6.44.0/release-notes/release-6-x#id-.Release6.xv6.44.0.

Tuncay Senturk _Snapbytes_
Community Champion
February 15, 2022

Thanks for the update @Vishal 

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
February 10, 2022

Hi @Vishal 

 

Are you getting an error/exception?

I checked the code and it seems OK at first glance.

Vishal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 11, 2022

I am not getting any error but I am not able to copy the value into Days field which is of type text field, also I am trying to do this on create field itself. But its not working.

Tuncay Senturk _Snapbytes_
Community Champion
February 11, 2022

The code seems ok, but I'd recommend adding several warn level logs after each line to see where the problem is. 

Suggest an answer

Log in or Sign up to answer