Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Shah Baloch September 9, 2022

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.

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 9, 2022

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

Shah Baloch September 12, 2022

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 12, 2022

Hi @Shah Baloch

Glad to hear the solution worked for you. :)

Please accept the answer.

Thank you and Kind regards,

Ram

Shah Baloch September 12, 2022

@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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 13, 2022

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
Shah Baloch September 13, 2022

It worked, thank you so much @Ram Kumar Aravindakshan _Adaptavist_ 

0 votes
Caoimhin Caldwell November 28, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

 

I was hoping you could help with a similar query . I am using your script to achieve the same thing but was wondering if there was a way to use it within a normal field where in a scheduled job could run each day to update the value of the field ( to accurately display the current date - custom date ) with the issues displayed on a dashboard . Do you know of a way I can achieve this ? 

Thanks 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 4, 2023

Hi @Caoimhin Caldwell

So, the scenario is pretty much the same. In your case, you will only need to use either the Custom Scheduled Job or Escalation Service, but you will need to specify which field you want to update, i.e., which Date field it is.

I suggest creating a separate question for this in the community and let me know by just adding an attn: to me so I can look into it.

Thank you and Kind regards,

Ram

Like Caoimhin Caldwell likes this
TAGS
AUG Leaders

Atlassian Community Events