Forums

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

Calculating custom date field based on system date

Suvidhaa Subramani July 15, 2022

Hi,

 

I have a requirement to create a new field to calculate ageing. I have below fields created,

Custom date field A

Scripted field B

Custom date field C

 

When Custom Date field A has a value, Scripted field B should start calculating the number of dates (that is A - System date) until Custom date field C gets a value

 

We have Script runner for JIRA plugin. Could you please help me in getting the script for this

2 answers

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.
July 18, 2022

Hi @Suvidhaa Subramani 

After going through your description and comment, what you are trying to do will not work 100% as you expect when using the Scripted Field, i.e. for the Ageing field.

For instance, if you want the auto calculation of the Scripted Field to take place, this is doable with:-

import com.atlassian.jira.component.ComponentAccessor

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

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

(sampleDateValue - currentDate) as Long

However, suppose you intend the Scripted Field to stop the calculation when the Actual Date is added. In that case, this will not work as you expect, i.e. even if the Actual Date is added, the Scripted Field will continue the calculation resulting in Negative Values.

You could add a condition something like:

if (!actualDateValue) {
(sampleDateValue - currentDate) as Long
}

Which will remove the value from the Scripted Field if the Actual Date is added.

If you intend to preserve the value from the Scripted Field, you could pass the Scripted Field value to another text field once the Actual Date field's value is added.

Below is the complete sample script for your reference:-

import com.atlassian.jira.component.ComponentAccessor

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

def actualDate = customFieldManager.getCustomFieldObjectsByName('Actual Date').first()
def actualDateValue = issue.getCustomFieldValue(actualDate)

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

if (!actualDateValue) {
(sampleDateValue - currentDate) as Long
}

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

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Suvidhaa Subramani July 20, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

It worked for my first scenario :)

Could you please help in below,

Once there is actualDatevalue, the calculation should stop and return the existing value even if it 1 or 2 or whatever it is

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.
July 22, 2022

Hi @Suvidhaa Subramani

If you go through my previous comment, I have already mentioned it will not work as you are expecting it to, i.e.:-
However, suppose you intend the Scripted Field to stop the calculation when the Actual Date is added. In that case, this will not work as you expect, i.e. even if the Actual Date is added, the Scripted Field will continue the calculation resulting in Negative Values.

You need to find a way to store the value before the field value is updated. For example, store it in a dummy table, invoke it again from the table, and display the result in the Scripted Field.

Even if you try to pass the value to a List, the List will reset when the Actual Date field value is set.

Thank you and Kind regards,

Ram

0 votes
Aditya Sastry
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.
July 15, 2022

Scripted field will calculate the value everyday once the custom date field A is selected?

What is expected once the custom date field C is populated?

Suvidhaa Subramani July 15, 2022

hi @Aditya Sastry 

Example:

I have a planned date field and Ageing field. Requirement is, if the planned date is today, then the document should be reviewed within 5 days.

So once the planned date is selected, then the Ageing field should calculate the number of days like Planned date - System date. It should get updated everyday.

Once the Actual date field gets value, then the counter of Ageing field should stop. Our clients will pull the Ageing column and make a report of issues which are more than 5 days.

Here Ageing field is my scripted field. Please let me know if you need more information

Suvidhaa Subramani July 18, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Could you please help me in this

Suggest an answer

Log in or Sign up to answer