Hi
I have the below script which works on a date field called "Target Date"
It requires tickets being created to have a 3 day leading time so they cannot set the target date to be today or in following 2 days as this is unrealistic expectation for delivery.
import
com.atlassian.jira.component.ComponentAccessor
import
com.atlassian.jira.user.util.UserUtil
import
java.util.Date.*
import
java.text.SimpleDateFormat
import
java.sql.Timestamp
import
java.text.DateFormat
duedatefield = getFieldByName(
"Target Date"
)
Date duedatevalue = (duedatefield.value
as
Date)
//today
def
today =
new
Date()
def
noticket =
new
Timestamp((
new
Date() +
3
).getTime())
if
(duedatevalue.getTime() < noticket.getTime()){
duedatefield.setError(
"Note: All tickets require a 3 day leading time, if your ticket is urgent and is required to be actioned the same day please contact xxx@yyy.com"
)
}
else
{
duedatefield.clearError()
}
I wanted to see if anyone can help me edit this script to only work on weekdays, ie - It will not factor in Saturday or Sunday. A example of the problem would be a user creating a ticket on Friday and setting the target date to be following Monday, where the team does not work the weekend, the earliest in this scenario should be the following Wednesday.
Thanks for any advice
Rich
For your requirement, you can try something like this:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def targetDate = getFieldById(fieldChanged)
def calendar = Calendar.instance
def map = [1: 3, 2: 3, 3: 3, 4: 5, 5: 5, 6: 5, 7: 4]
calendar.add(Calendar.DATE, map[calendar.get(Calendar.DAY_OF_WEEK)])
targetDate.clearError()
if (targetDate.value) {
def targetDateValue = targetDate.value as Date
if (targetDateValue < calendar.time) {
targetDate.setError('Note: All tickets require a 3 day leading time, if your ticket is urgent and is required to be actioned the same day please contact xxx@yyy.com')
}
}
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Behaviour configuration:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_
This is brilliant and works well.
How would I adapt this for 2 day lead time?
Can you offer a short explanation of how the below line works? I think I understand what is happening but just want to be sure.
def map = [1: 3, 2: 3, 3: 3, 4: 5, 5: 5, 6: 5, 7: 4]
Thanks so much again, great work!
Richard
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great to hear the solution worked for you. :-)
In your last comment, you asked:-
How would I adapt this for 2 day lead time?
So in the example that I have given, the map is set to 3 days lead time, i.e.:-
def map = [1: 3, 2: 3, 3: 3, 4: 5, 5: 5, 6: 5, 7: 4]
So basically, the map's key 1 is for Monday, 2 for Tuesday and so on.
For values, Monday till Wednesday, it is set to 3, while for Thursday, Friday, Saturday and Sunday, the value is set to 5 and 4, respectively, to exclude the weekends.
If you want to switch it to two days lead time, you will basically have to subtract all the values in the map by 1, i.e. it should be:-
def map = [1: 2, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 3]
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Ram,
Yes thats what I thought, what was confusing me was Jira considers Sunday to be the first day of the week not monday.
All good, thanks =)
Rich
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.