You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hello, I am trying to create a scriptrunner field based on a user request to calculate a deadline date which should be a number of business days after the due date - the number of days depends on the prioritisation of the issue.
I have a basic concept working but am falling down at the ensuring the number of days added are "business days" i.e. excluding weekends.
The code I have so far has been included below:
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
def customFieldManager = ComponentAccessor.customFieldManager
def AgreedDeliveryDate = customFieldManager.getCustomFieldObjectsByName("Agreed Delivery Date")[0]
def Prioritisation = customFieldManager.getCustomFieldObjectsByName("Prioritisation")[0]
def PrioritisationValue = issue.getCustomFieldValue(Prioritisation).toString()
def sdf = new SimpleDateFormat("yyyy-MM-dd")
def AgreedDeliveryDateValue = sdf.parse(issue.getCustomFieldValue(AgreedDeliveryDate).toString())
if(PrioritisationValue == "Urgent") {
AgreedDeliveryDateValue + 1
} else if(PrioritisationValue == "Priority") {
AgreedDeliveryDateValue + 2
} else if(PrioritisationValue == "Routine") {
AgreedDeliveryDateValue + 2
}
Any advice would be greatly welcomed.
Kind Regards,
David
My approach to something like that is to just evaluate the day of the week of the resulting date and if it's a saturday or sunday, I add 2 more days.
Something like this:
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
import java.time.DayOfWeek
def customFieldManager = ComponentAccessor.customFieldManager
def agreedDeliveryDate = customFieldManager.getCustomFieldObjectsByName("Agreed Delivery Date")[0]
def prioritisationCf = customFieldManager.getCustomFieldObjectsByName("Prioritisation")[0]
def prioritisationValue = issue.getCustomFieldValue(prioritisationCf).toString()
def sdf = new SimpleDateFormat("yyyy-MM-dd")
def agreedDeliveryDateValue = sdf.parse(issue.getCustomFieldValue(agreedDeliveryDate).toString())
def map = ['Urgent':1, 'Priority':2, 'Routine':2]
def returnDate = agreedDeliveryDate + map[prioritisationValue]
if(returnDate.toLocalDate().dayOfWeek in [DayOfWeek.SATURDAY, DayOfWeek.SUNDAY]){
returnDate + 2
}
I forgot to had, at the end of the entire script,
return returnDate
or just
reuturnDate
This will ensure that the final output of the script is the date you want.
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.
Hi @Peter-Dave Sheehan
I had a small error when using the above code, but was able to correct it by amending
def returnDate = agreedDeliveryDate + map[prioritisationValue]
to
def returnDate = agreedDeliveryDateValue + map[prioritisationValue]
I've also created a second field as the user actually had two requests to fulfill:
1. Initial Deadline
If Prioritisation = Urgent, Agreed Delivery Date +1, Prioritisation = Priority, Agreed Delivery Date +2, Prioritisation = Routine, Agreed Delivery Date +2,
2. Delivery Deadline
If Prioritisation = Urgent, Agreed Delivery Date +2, Prioritisation = Priority, Agreed Delivery Date +5, Prioritisation = Routine, Agreed Delivery Date +10,
I copied the code to a new scripted field and just amended the prioritisation values accordingly.
However, these fields don't appear to be working consistently and only return results in specific scenarios. I've run some tests, outcomes below:
1. Agreed Delivery Date on a Monday and Prioritisation = Urgent
Neither date is populated
2. Agreed Delivery Date on a Friday and Prioritisation = Urgent
Both fields appear to work correctly.
1. Agreed Delivery Date on a Monday and Prioritisation = Priority
Only Delivery Deadline is populated
1. Agreed Delivery Date on a Friday and Prioritisation = Priority
Only Initial Deadline is populated
1. Agreed Delivery Date on a Monday and Prioritisation = Routine
Neither date is populated
1. Agreed Delivery Date on a Friday and Prioritisation = Routine
Only Initial Deadline is populated
It appears that the field is only populated if the initial calculation falls on a weekend (before it adds extra days). How is this fixed?
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.