Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need a script that will validate a date is at least 3 business days in the future

Lynn Squire April 25, 2025

I can't figure this one out and it probably is easy for most of you. We want to create a script at the create issue step in the workflow that prevents an issue from being created using JSM form if that date is less than 3 business days in the future. We can figure out how to do 3 DAYS in the future but BUSINESS DAYS is giving me headaches. 

Anyone have a script they have used in the past I can steal? This is for JIra cloud. Again, this would prevent an issue from being created, would throw an error for the user if the date is less than 3 business days in the future using Scriptrunner.

This is the code I have so far, but it is failing. This is for Jira Cloud.

import java.time.LocalDate
import java.time.ZoneId
import java.time.DayOfWeek
import com.atlassian.jira.component.ComponentAccessor

// Get your custom field (Date Picker) — update with your actual field ID
def customField = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_12345")
def dateValue = issue.getCustomFieldValue(customField)

if (!dateValue) {
return false // Fail validation if no date was provided
}

// Convert java.util.Date to java.time.LocalDate
def targetDate = dateValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
def today = LocalDate.now()

// Count business days between today and the selected date
int businessDays = 0
def current = today

while (current.isBefore(targetDate)) {
if (!(current.getDayOfWeek() in [DayOfWeek.SATURDAY, DayOfWeek.SUNDAY])) {
businessDays++
}
current = current.plusDays(1)
}

// Pass only if target date is at least 3 business days ahead
return businessDays >= 3

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 30, 2025

Hi @Lynn Squire

After going through your description and subsequent comments, it appears  you are trying to use ScriptRunner for Jira Cloud's Behaviour to update the value on a JSM project. Please clarify this.

I am saying this because you mentioned Typescript error and Typescript  is used only for the Behaviour.

If yes, then unfortunately, at the moment, ScriptRunner for Jira Cloud's Behaviour doesn't support JSM Projects.

Please refer to this ScriptRunner for Jira Cloud Documentation for more information.

I am looking forward to your feedback and clarification.

Thank you and Kind regards,
Ram

0 votes
Trudy Claspill
Community Champion
April 25, 2025

Hello @Lynn Squire 

Welcome to the Atlassian community.

Are you getting an error message to indicate failure, or are you not getting the result you expect?

 

I have not analyzed your code, but I found another post marked as Solved that talks about getting the working days difference between two date custom fields. The code there might help you (though the post is tagged as for Jira Server).

https://community.atlassian.com/forums/Jira-questions/Script-Runner-Working-days-between-two-custom-date-fields/qaq-p/1050525

 

There is also an example from ScriptRunner of a script that calculates the days difference between two dates, though it does not include factoring in just business days.

https://www.scriptrunnerhq.com/help/example-scripts/calculate-difference-between-dates-cloud

 

Lynn Squire April 25, 2025

Error, something about the import function isn't allowed with Typescript. 

Trudy Claspill
Community Champion
April 25, 2025

Is that the exact error message? If not can you provide the exact and full error message, including information about which line is causing the error?

Piyush Annadate _ACE Pune_
Community Champion
April 26, 2025

This can't be a CLOUD Script, that's a DC Script as it has ComponentAccessor

Lynn Squire April 28, 2025

Ok, what would the right format be for cloud then? 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events