Use ScriptRunner to populate a date based on other custom field parameters.

David Vaughan November 10, 2022

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 

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2022

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
}
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2022

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.

David Vaughan November 14, 2022

@Peter-Dave Sheehan that's working perfectly, thanks for your help!

0 votes
David Vaughan November 11, 2022

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? 

Suggest an answer

Log in or Sign up to answer