Forums

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

Set the Due date based on created date, plus severity level days on business days

Andreas Kammer March 1, 2022

We are using script runner.

The needed behaviour is:

Everytime the severity level is changed, we need to set the due date based on the created date plus the level business days.

In short i need the following steps.

  • Get createdDate
  • Get severity timePeriod
  • Add days for weekends in createdDate + timePeriod
  • Update dueDate with added days

I found the following script but cannot get it to work with the created date and the business days.

import java.sql.Timestamp

def severityLevel = getFieldById(fieldChanged)

def severityLevelValue = severityLevel.value.toString()

def dueDateField = getFieldById("duedate")

def days = 24*60*60*1000 // number of milliseconds in a day

def updatedDate = new Date()

def timePeriod // to pass the number of days.

if (severityLevelValue == "1") {
timePeriod = 8
} else if (severityLevelValue == "2") {
timePeriod = 12
} else if (severityLevelValue == "3") {
timePeriod = 20
} else return null

updatedDate.setTime(new Timestamp(System.currentTimeMillis() + timePeriod * days).time)

dueDateField.setFormValue(updatedDate.format("dd/MMMM/yyyy"))

This script is based on current date and not the created date. It seems to me, that i cannot get the created date as timestamp format as basis for the calculation.

Also adding of days for the weekends is missing. I found this code snippet for the business days:

Calendar c1 = GregorianCalendar.getInstance();
c1.setTime(new Date())

for(int i=1;i<=timePeriod;i++){
c1.add(Calendar.DAY_OF_MONTH, 1);
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
c1.add(Calendar.DAY_OF_MONTH, 1);
if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
c1.add(Calendar.DAY_OF_MONTH, 1);
}

c1.clearTime().getTime().toTimestamp()

How can i combine this with the script before?

Your help is very much appreciated.

2 answers

1 vote
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 1, 2022

Hi @Andreas Kammer welcome on the community :). Your script is probably the behaviours script. So your use case is

  • user presses Create button
  • dialog appears
  • Due date is filled in
  • user can change it

In this case current date = create date so I'm not sure I understand it correctly. Can you describe it in more detail?

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 2, 2022

hi @Andreas Kammer ok, so just few examples for me to understand it better

  • Severity is set as
    • High - 1day
    • Medium - 2days
    • Low - 4days
  • case 1
    • user displays Create dialog (create date is March 1st 2022)
    • user selects Severity = Medium
    • Due Date on the form is updated to March 3rd 2022
    • user selects Severity = High
    • Due Date on the form is updated to March 2nd 2022
    • user can manually adjust the Due Date
  • case 2
    • issue is created with
      • create date = March 5th 2022
      • Severity = Medium
      • Due Date = March 7th 2022
    • user Edits an issue and sets Severity = high
    • Due date on the dialog is updated to March 6th 2022
    • user can manually adjust the Due Date

Is it ok?

Andreas Kammer March 2, 2022

Yes, but it needs to be business days (Mo-Fr).

So in case 2 Due date would be initally March 8th and then updated to March 7th.

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 3, 2022

Hi @Andreas Kammer now I understand it perfectly... sorry, I'm sometimes little bit slow :).

In this case your only problem is probably really that you need switch current date for created date. You can get created date from the issue. In behaviours there is a variable 

underlyingIssue

So you can get created date using following method (https://docs.atlassian.com/software/jira/docs/api/8.22.0/com/atlassian/jira/issue/Issue.html#getCreated--) as Timestamp

underlyingIssue.getCreated()

 More information about underlyingIssuehttps://docs.adaptavist.com/sr4js/latest/get-help/frequently-asked-questions/behaviours-faqs

The code to get correct date as String could be something like (I didn't test it because I do not have running environment):

 


import java.sql.Timestamp
import java.time.DayOfWeek
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

severityTimePeriods = ["1": 8, "2": 12, "3": 20] as Map<String, Integer>

Timestamp currentDate = new Timestamp(new Date().getTime())

def newDueDate = getDueDate(currentDate, "3")

def newDueDateAsString = newDueDate.format(DateTimeFormatter.ofPattern("dd/MMMM/yyyy"))

LocalDateTime getDueDate(Timestamp createdDate, String severity) {
LocalDateTime dueDate = createdDate.toLocalDateTime()

int i = 0
while(i < severityTimePeriods.get(severity)) {
if (dueDate.getDayOfWeek() == DayOfWeek.SATURDAY || dueDate.getDayOfWeek() == DayOfWeek.SUNDAY) {
// nothing
}else{
i++
}
dueDate = dueDate.plusDays(1)
}

return dueDate
}

Let me know if you need more assistance :)

Andreas Kammer March 4, 2022

Thank you very much for your help!

There is a problem with the severityTimePeriods.get(severity) in the while loop.

Can you help me with that?

Error.png

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 4, 2022

Hi @Andreas Kammer it is just static code check but it should work. I finally tested it in Groovy console. Did you try to execute the behaviours?

Asiya fathima August 17, 2023

Hi @Martin Bayer [MoroSystems, s.r.o.] , Can you please help to find due date only based on created date?

0 votes
Andreas Kammer March 1, 2022

Thanks and yes, it is a behaviour script. The severity can change afterwards or is set after creation of the issue and the due date should still be set based on creation date.

Suggest an answer

Log in or Sign up to answer