Forums

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

Scriptrunner field behavior depend on Date fields comparison

Narek Zohrabyan August 3, 2021

Hi,

We need to make a field mandatory if the Start Date didn't pick up five working days earlier. Otherwise, if the condition is true field must be hidden from the reporter. If someone met up the same or similar problem?

I appreciate any help you can provide.

1 answer

0 votes
Vikrant Yadav
Community Champion
August 3, 2021

Hi @Narek Zohrabyan  If i am understanding it correctly , you want to hide another field ( may i know type of this field) , based on this condition like Start Date = CurrentDate < -5 days ? 

 

Thanks

Vikrant Yadav

Narek Zohrabyan August 3, 2021

Hello @Vikrant Yadav ,

Thank you for your response.

Condition is true until the Start Date >= Current Date - 5 working days (skipping weekends). The custom field type will be a checkbox and hidden until the condition won't be true and it will be required.

Thanks

Vikrant Yadav
Community Champion
August 5, 2021

Hi @Narek Zohrabyan

You can use below code to hidding and unhiding checkbox field . For weekends, @PD Sheehan @Ram Kumar Aravindakshan _Adaptavist_  Kindly suggest..i am not sure how to check for weekends. 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.time.DayOfWeek

@BaseScript FieldBehaviours fieldBehaviours

def dateField = getFieldById(fieldChanged)//Start Date
def dateValue = dateField.value as Date


def checkbox_field = getFieldById("customfield_23445")

//def weekend = EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)

def today = new Date().minus(6)

if (dateValue > today) {
checkbox_field.setHidden(false)
} else {
checkbox_field.setHidden(true)
checkbox_field.setRequired(true)

}

 Thanks

Like desarrollo likes this
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 5, 2021

Here is a short function that can return the date based on a number of business days ago

import groovy.time.TimeCategory
def busDayAgo(startDate, nDays){
def ago=startDate
(0..nDays).each{
use(TimeCategory){
ago = ago.minus(1.day)
(ago.day in [0,6]) && (ago = ago.minus(1.day))
}
}
ago
}

busDayAgo(new Date(),5)

Suggest an answer

Log in or Sign up to answer