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,361,493
Community Members
 
Community Events
168
Community Groups

Scripted Field Error When try to calculate between current date and a custom field date

Hi

I'm trying to create a scripted number field to calculate the number of days between a custom date field (deadline) and the current date, however, I'm getting a "No signature of method" error.  

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.fields.ImmutableCustomField.minus() is applicable for argument types: (Date) values: [Fri Sep 09 17:51:34 MST 2022] Possible solutions: find(), find(groovy.lang.Closure), is(java.lang.Object), any(), use([Ljava.lang.Object;), print(java.lang.Object) at Script118.run(Script118.groovy:17)

When I ran the custom date field code in the console, like this:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def deadline = customFieldManager.getCustomFieldObject("customfield_13201")
return deadline

it returned the custom field name "Deadline". However when I run the current date code:

def currentDate = new Date()
return currentDate

It returns the result "Fri Sep 09 18:05:26 MST 2022".

When checking one of the issues the Deadline custom date field value is displayed like this "10/Sep/22".

I think the error might be related to the date format. I don't know how to fix it. Can someone help me to fix the issue? Here is the code

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
import org.apache.log4j.Logger
import java.util.Date
import com.atlassian.jira.issue.fields.ImmutableCustomField
import com.atlassian.jira.issue.fields.CustomField

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def deadline = customFieldManager.getCustomFieldObject("customfield_13201")
def currentDate = new Date()

if (deadline != null) {
def daysDiff = deadline - currentDate
return daysDiff
}
return null

Thank you for your help.

1 answer

1 accepted

2 votes
Answer accepted

Hi @Shah Baloch

For your requirement, you could try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import java.util.concurrent.TimeUnit

def customFieldManager = ComponentAccessor.customFieldManager
def sampleDate = customFieldManager.getCustomFieldObjectsByName('Sample Date').first()
def sampleDateValue = issue.getCustomFieldValue(sampleDate) as Date

def currentDate = new Date(System.currentTimeMillis())

if (sampleDateValue) {
def dateBeforeInMs = sampleDateValue.time
def dateAfterInMs = currentDate.time
def timeDiff = Math.abs(dateAfterInMs - dateBeforeInMs)

TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS)
}

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Scripted Field configuration:-

scripted_field_config.png

As shown in the screenshot above, the Scripted Field type used is the Number Field.

Also, I include a few test screenshots for your reference:-

1. First, I create a new issue and set a date for the Sample Date field. For this example, the date field has been set to 1st September 2022. 

test1.png

2. Once the ticket is created, as expected, the Scripted Field calculates the difference between the date set in the Sample Date field to the Current date (10th September 2022). 

As expected, the result displayed in the Scripted Field is 9.

test2.png

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

Hi, @Ram Kumar Aravindakshan _Adaptavist_ it works thank you. I tried to do it on the opposite but still got the same positive numbers. I thought it'll show negative. I mean try to minus the current date from the deadline custom date. For example, the deadline date is 01/Sep and the Current date is 12/Sep. I was expecting to see -11 instead of 11. Here is the change I made.

 def timeDiff = Math.abs(dateBeforeInMs - dateAfterInMs)

Thank you for your help.

Hi @Shah Baloch

Glad to hear the solution worked for you. :)

Please accept the answer.

Thank you and Kind regards,

Ram

@Ram Kumar Aravindakshan _Adaptavist_My apology didn't explain it well. I was trying to ask if is it possible to take out a higher number from a smaller number. I mean I just want to have the difference between positive and negative. For example, if the deadline is in the future the positive number but the deadline date is past then should display a negative number. In that way, we know how many days we were late from the deadline. For example, if the deadline was 01/Sep and today is the 12th then the number should be negative but if the deadline is 23/Sep and today is the 12th then the number should be positive. In your given code regardless of the change order of the timeDiff code it still shows a positive number.

How can I acheive that?

Thank you for your help.

Hi @Shah Baloch

If you want to return a negative value for dates that have exceeded the due date, you can just add a basic multiplication of the result with -1. For example:-

import com.atlassian.jira.component.ComponentAccessor
import java.util.concurrent.TimeUnit

def customFieldManager = ComponentAccessor.customFieldManager
def sampleDate = customFieldManager.getCustomFieldObjectsByName('Sample Date').first()
def sampleDateValue = issue.getCustomFieldValue(sampleDate) as Date

def currentDate = new Date(System.currentTimeMillis())

if (sampleDateValue) {
def dateBeforeInMs = sampleDateValue.time
def dateAfterInMs = currentDate.time
def timeDiff = Math.abs(dateAfterInMs - dateBeforeInMs)
def result = TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS)

if (dateAfterInMs > dateBeforeInMs) {
result * -1
} else {
result
}
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are a few test screens for your reference:-

1. If the Date in the date field has been set for a later date, the result will be a positive number, as shown below:-

image1.png

2. And if the date in the date field has been set for an earlier date, the result will be a negative number, as shown below:-

image2.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram 

Like Shah Baloch likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

208 views 1 6
Read article

Atlassian Community Events