Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,560,218
Community Members
 
Community Events
185
Community Groups

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

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.
Nov 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.
Nov 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.

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

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
TAGS
AUG Leaders

Atlassian Community Events