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.
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.
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.
Hi @Andreas Kammer welcome on the community :). Your script is probably the behaviours script. So your use case is
In this case current date = create date so I'm not sure I understand it correctly. Can you describe it in more detail?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @Andreas Kammer ok, so just few examples for me to understand it better
Is it ok?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 underlyingIssue: https://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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Martin Bayer _MoroSystems_ s_r_o__ , Can you please help to find due date only based on created date?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.