Calculate duration from cascading fields

Mariam Mohamed November 5, 2024

I have two cascading fields (start date and end date) both of them have month -> year values, for example: Feb - 2025

I need to calculate the duration between start date and end date then update another field (category) as follows:

Small: 1 month duration

Medium: 2 month duration

 

I have script runner and jira automation but I'm not sure how to achieve this

1 answer

1 accepted

0 votes
Answer accepted
Zuri Suwru
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.
November 5, 2024

Hey there Mariam,

A very ugly and hardcoded example to do this with ScriptRunner would work via a listener, by separating the cascading field to it's year and month, and assigning an integer value to both.

After doing so, you would simply subtract the end from the start, leaving you with a value representing the time left. This you could use to set your field's value afterwards.

A listener script watching "Create Issue" and "Update Issue" events:

//Get the field values from the event
def startDateMap = event.issue.getCustomFieldValue(22200) as Map //The number in the brackets represent the custom field ID
def endDateMap = event.issue.getCustomFieldValue(22201) as Map

//Access and store the two keys inside the maps
def startDateYear = startDateMap.get(null) as String
def startDateMonth = startDateMap.get("1") as String
def endDateYear = endDateMap.get(null) as String
def endDateMonth = endDateMap.get("1") as String

//Simple but hardcoded logic for months
def startMonthValue
def endMonthValue
if(startDateMonth == "January") {
    startMonthValue = 1
}
else if (startDateMonth == "February") {
    startMonthValue = 2
}
else if (startDateMonth == "March") {
    startMonthValue = 3
}
else if (startDateMonth == "April") {
    startMonthValue = 4
}
else if (startDateMonth == "May") {
    startMonthValue = 5
}
else if (startDateMonth == "June") {
    startMonthValue = 6
}
else if (startDateMonth == "July") {
    startMonthValue = 7
}
else if (startDateMonth == "August") {
    startMonthValue = 8
}
else if (startDateMonth == "September") {
    startMonthValue = 9
}
else if (startDateMonth == "October") {
    startMonthValue = 10
}
else if (startDateMonth == "November") {
    startMonthValue = 11
}
else if (startDateMonth == "December") {
    startMonthValue = 12
}
else {
    startMonthValue = 0
}

if(endDateMonth == "January") {
    endMonthValue = 1
}
else if (endDateMonth == "February") {
    endMonthValue = 2
}
else if (endDateMonth == "March") {
    endMonthValue = 3
}
else if (endDateMonth == "April") {
    endMonthValue = 4
}
else if (endDateMonth == "May") {
    endMonthValue = 5
}
else if (endDateMonth == "June") {
    endMonthValue = 6
}
else if (endDateMonth == "July") {
    endMonthValue = 7
}
else if (endDateMonth == "August") {
    endMonthValue = 8
}
else if (endDateMonth == "September") {
    endMonthValue = 9
}
else if (endDateMonth == "October") {
    endMonthValue = 10
}
else if (endDateMonth == "November") {
    endMonthValue = 11
}
else if (endDateMonth == "December") {
    endMonthValue = 12
}
else {
    endMonthValue = 0
}

//Calculate the difference and get the result in months

log.warn(((endDateYear.toInteger() - startDateYear.toInteger()) * 12) + (endMonthValue-startMonthValue))

Using this example, if the start date is 2024 January, and the end date is 2025 February, the result is "13", the difference in months.

Note that I'm only logging out the value here, but you could hook up logic to the latter to set your field in question.

Hope this helped getting an idea on how you could resolve your issue at hand!

Best Regards,
Zuri Suwru

 


PS.: Why not just use date fields? I would personally double check if cascading fields are really necessary first.

Mariam Mohamed November 5, 2024

Hi @Zuri Suwru I used your code as custom field and I did minor changes here and there and it worked fine.. thank you! now I just need to do the logic for calculating the months

overall what you shared got me set up, I really appreciate your effort.. thanks again!!

note I also want to use date fields as it will be piece of cake but the project is already up for 1 year and now we need this automation :')

Suggest an answer

Log in or Sign up to answer