Calculated Field (SR) question - calculating "days old"

Lana Decker May 10, 2023

I have a script  that works perfectly fine:

def createDate = issue.getCreated();
def today = new java.sql.Timestamp(new Date().getTime());
def resolutionDate = issue.getResolutionDate();
if (resolutionDate == null) {
   return today - createDate;
}
if (resolutionDate !== null) {
   return resolutionDate - createDate;
}

(This script calculates how old is the Risk issue if open, or how old it was when it was resolved.)

Something that is very similar fails every time:

def issueDate = issue.getCustomFieldValue(
customFieldManager.getCustomFieldObject("customfield_10802"))
def today = new java.sql.Timestamp(new Date().getTime());
def resolutionDate = issue.getResolutionDate();
if (resolutionDate == null) {
   return today - isssueDate;
}
if (resolutionDate !== null) {
   return resolutionDate - issueDate;
}

 (This script calculates how old is the Actualized Risk issue if open, or how old it was when it was resolved.  "customfield_10802" is the "actualised Risk aka Issue Date.)

The error i get is "2023-05-10 08:21:23,097 ERROR [customfield.GroovyCustomField]: Script field failed on issue: AECM-240, field: # days as ISSUE groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script64 at Script64.run(Script64.groovy:2)"

What am I doing wrong?

Days as Issue.jpg

1 answer

0 votes
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.
May 10, 2023

Hi @Lana Decker 

I have reviewed your code, and there is an error because you have not declared the customFieldManager. The customFieldManager is not a bound variable which you can declare directly. Hence, you will need to initialise it first.

To know what are the bound variables for the Scripted Field, you will need to click on the blue question mark icon as shown in the image below:-

image1.pngimage2.png

If you observe the screenshots above, there is no bound variable for the customFieldManager. This is the main cause of the error you are incurring.

Please modify your code as shown below and see if it helps.

import java.sql.Timestamp
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def customField1 = customFieldManager.getCustomFieldObject("customfield_10802")
def issueDate = issue.getCustomFieldValue(customField1) as Date
def today = new Timestamp(System.currentTimeMillis())
def resolutionDate = issue.resolutionDate

if (!resolutionDate) {
today.minus(issueDate)
}

if (resolutionDate) {
resolutionDate.minus(issueDate)
}

Also, could you please specify what type of field you are using, customfield_10802? I am requesting this to provide a sample code for your reference.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

Lana Decker May 11, 2023

Thank you for looking into this, Ram!
10803 is a date field (Issue date, or when risk was actualized and became issue)

I did copy-paste with your script and it couldn't compile

Error.jpg

10802 was a typo, so i blame me for this one
10802 was a check list field (issue y/n)

Lana Decker May 11, 2023
Ram,
Thank you for helping me make progress.
Partially working.jpg
It now only works IF issue is resolved. 
If its still unresolved - there is no number:
Example.jpg
Any ideas?

Suggest an answer

Log in or Sign up to answer