Forums

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

How do I make a custom field required based on due date

Dave Cuff September 5, 2020

Hi Folks,

 

I am trying to make a custom field required based on the value entered into duedate.  I am trying this script with a behaviour but it does not seem to be working.  Any ideas.

// required import statements
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*

// Get pointers to my date field and get its value as a date
def cf = getFieldByName("Due Date")
def cfval = cf.getValue() as Date
def cf2 = getFieldByName("Justify the level of the urgency")
def cfval2 = cf.getValue()

// get todays date
def today = new Date()

// get the date three months in the future
def twoMonthDate = today.plus(60)

// if the date entered is before todays throw an error
if(cfval.before(twoMonthDate)) {
cf2.setError("You must Justify the level of urgency")
cf2.setRequired(true)
}else{
cf2.clearError()
cf2.setRequired(false)
}

1 answer

0 votes
Gustavo Félix
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.
September 5, 2020

Hi @Dave Cuff 
Have you try a workflow Validator? I think it fits better on what you need.

e.g. based on your code

// if the date entered is before todays throw an error
if(cfval.before(twoMonthDate) && cfval2==null) {
throw new InvalidInputException("You must Justify the level of urgency")
}

Something like that should work.
extra library for the exception:
import com.opensymphony.workflow.InvalidInputException


https://confluence.atlassian.com/adminjiraserver/advanced-workflow-configuration-938847443.html

Dave Cuff September 8, 2020

Hi Thanks for the feedback.

I have now tried this as a validator in the workflow.  This is the rule I am trying to validate.
If the dueDate is less than two months in the fyture then the "justofy the level of urgency" field cannot be null.

This script however does not work.  I am always getting a false return no matter what the dueDate is - below is my code.

if ( cfValues['Justify the level of the urgency'] == null && issue.dueDate?.before(Calendar.getInstance().getTime() + 60) ) {
return false
}

Gustavo Félix
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.
September 8, 2020

You dont have to add a return .

Based on your original code:

def cf = getFieldByName("Due Date")
def cfval = cf.getValue() as Date
def cf2 = getFieldByName("Justify the level of the urgency")
def cfval2 = cf.getValue()

// get todays date
def today = new Date()

// get the date three months in the future
def twoMonthDate = today.plus(60)

if(cfval.before(twoMonthDate) && cfval2==null) {
throw new InvalidInputException("You must Justify the level of urgency")
}
By throwing the InvalidInputException, your issue wont be created, and the message "You must justify the level of urgency" should appear on your screen.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events