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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,558,428
Community Members
 
Community Events
184
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
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.
Sep 09, 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

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.
Sep 12, 2022

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.

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events