Target Date Weekdays Only (Behaviour)

Richard Duffy March 28, 2023

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

 

1 answer

1 accepted

1 vote
Answer accepted
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.
March 29, 2023

Hi @Richard Duffy

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:-

image1.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Richard Duffy March 30, 2023

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 

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.
March 30, 2023

Hi @Richard Duffy 

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

Richard Duffy April 2, 2023

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.1
TAGS
AUG Leaders

Atlassian Community Events